如何根据变量 (VBA) 更改 table 搜索的范围
How to change the scope of a table search depending on a variable (VBA)
我有 A1 =1,这是 table 的数量。如果单元格中的值发生变化 - 添加一个新的 table。我有一个搜索它的宏(下面的代码)。如果我知道如何让它搜索范围:
- 每个 table 之间的距离是恒定的(5 个空单元格)
- table 目前有固定值(但将来会改变)
- 我知道 table 的数量
我正在寻找一种方法,对于每个下一个 table (A1),搜索范围都会更改为这个添加的范围。
我会特别寻求有关设置 .Range.
的帮助
提到的代码:
Sub pulling_row_number_if_it_finds_the_code_in_the_table()
Dim my_cell As Object
Dim nr_row_code_found As Integer
Dim my_Range As Range
With Worksheets("Sheet1")
Set my_Range = Range("A5:A50")
For Each my_cell In my_Range
If my_cell.Value = .Range("B1").Value Then
nr_row_code_found = my_cell.Row
.Range("F1") = nr_ row_code_found
End If
Next my_cell
End With
End Sub
如果表格大小相同:
Sub pulling_row_number_if_it_finds_the_code_in_the_table()
Dim my_Range As Range, m, tblNum As Long
Dim rngT1 As Range
With Worksheets("Sheet1")
Set rngT1 = .Range("A5:A50") 'first table
tblNum = .Range("A1").Value
Set my_Range = rngT1.Offset((tblNum - 1) * (rngT1.Rows.Count + 5))
m = Application.Match(.Range("B1").Value, my_Range, 0)
If Not IsError(m) Then 'if got a match
.Range("F1") = my_Range.Cells(m).Row
Else
.Range("F1") = "no match"
End If
End With
End Sub
我有 A1 =1,这是 table 的数量。如果单元格中的值发生变化 - 添加一个新的 table。我有一个搜索它的宏(下面的代码)。如果我知道如何让它搜索范围:
- 每个 table 之间的距离是恒定的(5 个空单元格)
- table 目前有固定值(但将来会改变)
- 我知道 table 的数量
我正在寻找一种方法,对于每个下一个 table (A1),搜索范围都会更改为这个添加的范围。 我会特别寻求有关设置 .Range.
的帮助提到的代码:
Sub pulling_row_number_if_it_finds_the_code_in_the_table()
Dim my_cell As Object
Dim nr_row_code_found As Integer
Dim my_Range As Range
With Worksheets("Sheet1")
Set my_Range = Range("A5:A50")
For Each my_cell In my_Range
If my_cell.Value = .Range("B1").Value Then
nr_row_code_found = my_cell.Row
.Range("F1") = nr_ row_code_found
End If
Next my_cell
End With
End Sub
如果表格大小相同:
Sub pulling_row_number_if_it_finds_the_code_in_the_table()
Dim my_Range As Range, m, tblNum As Long
Dim rngT1 As Range
With Worksheets("Sheet1")
Set rngT1 = .Range("A5:A50") 'first table
tblNum = .Range("A1").Value
Set my_Range = rngT1.Offset((tblNum - 1) * (rngT1.Rows.Count + 5))
m = Application.Match(.Range("B1").Value, my_Range, 0)
If Not IsError(m) Then 'if got a match
.Range("F1") = my_Range.Cells(m).Row
Else
.Range("F1") = "no match"
End If
End With
End Sub