我正在尝试在两个单独的文件中验证列值。这两个文件的文件路径位于同一工作表中的两个单独的单元格中。我让它为一个文件工作,但我需要在两个单独的表中验证列值。这是我到目前为止所尝试的。
但是,我收到错误消息。 “编译错误:vInvalid Next控件变量引用”并突出显示Next x
。为什么呢?
Sub ButtonSearch_Click()
Application.ScreenUpdating = False
' Name of columns to be found
JiraColumns = Array("CRM#", "status", "Key", "Resolution", "Assignee", "Customer Name", "Company")
SparksColumns = Array("QCCR", "Hpsw Status")
' Define boundaries
For x = LBound(JiraColumns) To UBound(JiraColumns)
For y = LBound(SparksColumns) To UBound(SparksColumns)
' Search for column names on general_report Sheet
With oWB.Sheets("general_report")
Set foundRange = .Cells.Find(What:=JiraColumns(x), After:=.Cells(1, 1), _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
End With
If foundRange Is Nothing Then
' Missing Column
intRow = 1
MsgBox "Jira is missing Column: " & (JiraColumns(x))
oWB.Close False
Exit Sub
Else
Count = Count + 1
End If
Next x
' *** *** Sparks verification *** *** SECOND FILE, SECOND FILE, SECOND FILE *** *** ***
With sWB.Sheets("Sample-Sparks")
Set foundR = .Cells.Find(What:=SparksColumns(y), After:=.Cells(1, 1), _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
End With
If foundR Is Nothing Then
' Missing Column
intRow = 1
MsgBox "Sparks is missing Column: " & (SparksColumns(y))
sWB.Close False
Exit Sub
Else
CountS = CountS + 1
End If
Next y
If Not foundRange Is Nothing Then foundRange.Activate
If Not foundR Is Nothing Then foundR.Active
' close the source workbook without saving any changes
oWB.Close False
' free memory
Set wbFind = Nothing
' Show activity on screen
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:4)
你混淆了你的下一个循环。你的y循环应该嵌套在你的x循环中。即。
For x = LBound(JiraColumns) To UBound(JiraColumns)
For y = LBound(SparksColumns) To UBound(SparksColumns)
'...code...
Next y
'...code...
Next x
关于行SFile = Worksheets("Sheet1").Range("F17").Value
中的错误的另一个问题是因为您在此之前的行中打开了JFile
路径上的文件。因为JFile
现在是您ActiveWorkook
,并且该工作簿不包含名为Sheet1
的工作表,您会收到错误消息。
在使用多个工作簿时,您需要注意哪个工作簿是活动工作簿。您可以声明工作簿(在本例中为ActiveWorkbook
):
Dim ActWB As Workbook
Set ActWB = ActiveWorkbook
然后获取sFile
值,如:
sfile = ActWB.Sheets("Sheet1").Range("F17").Value
同样可以引用JFile
中的单元格(例如):oWB.sheets("Test").range("A1").value