在另一个 sheet 上匹配一个单元格

Matching a cell on another sheet

我编写了下面的代码来匹配另一个单元格中的一个单元格 sheet 这样我就可以在需要的地方偏移和粘贴更多数据。

代码似乎只能找到 sheet 的第一行,而不是具有值的单元格。

Sub Find_select()
  
    Dim matchValue As Variant, FindCell As Long
   
    Sheets("Booking Sheet").Activate
      
    matchValue = Worksheets("Booking Sheet").Range("BE5").Value
    FindCell = WorksheetFunction.Match(matchValue, Worksheets("Data Storage").Range("A2:A10000"), 0)
    
    Sheets("Data Storage").Activate
    ActiveSheet.Cells(FindCell).Select
End Sub

要获得您想要的结果,您需要做的最小更改是:

ActiveSheet.Cells(FindCell,1).SelectActiveSheet.Cells(FindCell,"A").Select

基本上,当您使用 .cells 集合时,您需要为 VBA 指定行索引和列索引,以了解您指的是哪个单元格。

ActiveSheet.Cells(FindCell) 指索引为 FindCell 的列的第一个单元格,因此例如,如果 FindCell=10 那么您的代码将 select 单元格 J1.

话虽如此,我也会避免在没有特别需要时使用 .Activate 方法和 ActiveSheet 对象,而是使用显式引用。

Dim matchValue As Variant, FindCell As Long
Dim sht1 As Worksheet, sht2 As Worksheet
Set sht1 = ThisWorkbook.Worksheets("Booking Sheet")
Set sht2 = ThisWorkbook.Worksheets("Data Storage")

matchValue = sht1.Range("BE5").value
FindCell = Application.WorksheetFunction.Match(matchValue, sht2.Range("A2:A10000"), 0)
sht2.Cells(FindCell, "A").Select

此外,我不确定您为什么要使用 .select 方法,但很可能有更好的方法可以在没有它的情况下获得相同的结果。