1

我正在尝试检查 中的值是否workbook1-sheet1-Aworkbook2-sheet2-E. 如果有匹配则workbook1-sheet1-Y接收一个'x'.

这是我到目前为止的代码,并且Run time error 424 - Object required在 if 循环中给了我错误。

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
4

1 回答 1

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
于 2012-10-30T14:22:56.333 回答