我正在尝试检查workbook1-sheet1-A
中workbook2-sheet2-E
中的值是否匹配。如果匹配workbook1-sheet1-Y
,则会收到'x'
。
这是我到目前为止的代码,在if循环上给出了Run time error 424 - Object required
错误。
Sub Check()
'
Application.ScreenUpdating = False
endRow = Range("A" & Rows.Count).End(xlUp).Row
wkbSLA = ("F:\Workbook2")
For i = 2 To endRow
If Cells(i, 1).Value = wkbSLA.Sheets("Sheet2").Range("E2:E575").Value Then
Range("Y" & i) = "x"
End If
Next i
'
End Sub
答案 0 :(得分:0)
为了确保您的代码正在执行您期望的所有操作,请确保使用变量限定对象。
此外,您无法在多单元格范围内使用.Value
属性。这可能是你的错误发生的地方。更好的方法是使用.Find
方法。
如果您编写类似的代码,它应该适合您...虽然您可能需要调整一下以满足您的需求。
Option Explicit
Sub Check()
Application.ScreenUpdating = False
Dim wkb1 as Workbook, wkb2 as workbook
Set wkb1 = Workbooks("workbook1")
Set wkb2 = Workbooks("F:\Workbook2")
' -> do you need Workbooks.Open("F:\Workbook2") here?
' Seems you may if the workbook you want to access is closed
Dim wks1 as Worksheet, wks2 as Worksheet
Set wks1 = wkb1.Sheets(1) 'may need to change this reference for your needs
Set wks2 = wkb2.Sheets("Sheet2")
With wks1
Dim endRow as Long
endRow = .Range("A" & Rows.Count).End(xlUp).Row
Dim i as Long
For i = 2 To endRow
Dim rng as Range
Set rng = wks2.Range("E2:E575").Find .Cells(i,1).Value lookat:=xlWhole
If Not rng Is Nothing Then .Range("Y" & i) = "x"
Next i
End With
End Sub