excel - 程序无输出

标签 excel vba

我有 6 个工作表。其中第一个在第 6 列(第 13 行到第 29 行)中有一个 id 列表(例如,1.1.3、1.2.3、1.3.3 等),我希望将其与其他四张工作表上的第 2 列中的值相匹配。如果我找到匹配项,我想在包含 id 的行的第一个空单元格中写入包含匹配项的工作表的名称。注意 在一个或多个正在搜索的工作表中,单个 ID 可能有多个匹配项。

例程运行没有错误,但没有输出。有人可以帮忙吗?

这就是我所拥有的。

Private Sub Command_MapComp_Click()

    Dim BP As Worksheet
        Set BP = Sheets("Blue Print")
    Dim CF As Worksheet
        Set CF = Sheets("Competency Framework")
    Dim lr As Long
    Dim lc As Long
    Dim i As Long
    Dim j As Long
    Dim FindVal As String

'Loop through each cell in Column 6 from Row 13 to 29 and store its value in the "findVal" variable

    
    For i = 13 To 29
        FindVal = BP.Cells(i, 6).Value
'Locate Last empty cell in Row "i":
        lc = BP.Cells(i, Columns.Count).End(xlToLeft).Column + 1
    
'Loop each sheet in the workbook

        For Each ws In ThisWorkbook.Worksheets
    
            If ws.Name = BP.Name Or ws.Name = CF.Name Then
          
'Do nothing
            Else

'Loop each cell in Column 2 in ws from Row 8 to Last Row to find the "FindVal"
  variable
    
                lr = ws.Cells(Rows.Count, 1).End(xlTUp).Row
        
                    For j = 8 To lr
                        If Cells(j, 2).Value = FindVal Then
    
'Enter the name of the Worksheet in the first empty cell in Row "i"
    
                        BP.Cells(i, lc).Value = ws.Name
    
                    Next j
    
            End If
    
        Next
   Next i
    
    End Sub

最佳答案

在工作表中循环查找值时,您没有指定要扫描的工作表 - 因此它只会扫描事件工作表。

If Cells(j, 2).Value = FindVal Then 

应该是

If ws.Cells(j, 2).Value = FindVal Then

此外,正如评论中所指出的,存在一个拼写错误的小问题,应该会导致错误,更不用说 IF block 结构问题了。最后,您没有在循环内重新计算 lc ,因此如果一个值位于多个工作表上,它将覆盖而不是追加。我已经在此处的代码中解决了这些问题并对其进行了测试 - 它工作正常,没有错误 - 它执行您所描述的操作并输出工作表名称。

 Private Sub Command_MapComp_Click()

    Dim BP As Worksheet
    Dim CF As Worksheet
    Dim lr As Long
    Dim lc As Long
    Dim i As Long
    Dim j As Long
    Dim FindVal As String

    Set BP = Sheets("Blue Print")
    Set CF = Sheets("Competency Framework")

    'Loop through each cell in Column 6 from Row 13 to 29 and store its value in the "findVal" variable
    For i = 13 To 29
    
        FindVal = BP.Cells(i, 6).Value
        
        'Loop each sheet in the workbook
        For Each ws In ThisWorkbook.Worksheets
        
            'Locate Last empty cell in Row "i":
            lc = BP.Cells(i, Columns.Count).End(xlToLeft).Column + 1
        
            If ws.Name = BP.Name Or ws.Name = CF.Name Then
          
            'Do nothing
            
            Else
        
                'Loop each cell in Column 2 in ws from Row 8 to Last Row to find the "FindVal" variable
                lr = ws.Cells(Rows.Count, 2).End(xlUp).Row
                    For j = 8 To lr
                        If ws.Cells(j, 2).Value = FindVal Then
        
                            'Enter the name of the Worksheet in the first empty cell in Row "i"
                            BP.Cells(i, lc).Value = ws.Name
                        
                    End If
               Next j
            End If
        Next
    Next i
    
End Sub

关于excel - 程序无输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76438269/

相关文章:

events - 如何使用 VBA 将事件添加到运行时在 Excel 中创建的控件

excel - 允许用户选择 "print PDF"的保存文件路径

python - 在Python中按日期过滤Excel中的数据

database - 将 BLOB 存储在数据库中是个好主意吗?

.net - 如何结束excel.exe进程?

excel - 动态网页查询

python - 无论我做什么,都无法将 XLWings 加载项安装到 excel 中

excel - 当工作表受到保护时,宏不起作用。运行宏返回运行时错误 1004

php - 如何使用 Box\Spout 获取 Excel 工作表的单行

excel - Excel VBA中的嵌套循环错误