如何选择满足特定行中特定列的值的所有行

时间:2017-07-30 02:59:05

标签: vba

我正在编写一个代码,它将执行以下操作: 选择活动单元格后,我想在表格中选择与D列中活动行的值匹配的其他行。例如,如果活动行的值为" Sea"在D栏中,我想循环遍历所有行并选择那些有" sea"在D栏。

我正在寻找的SQL等价物:

SELECT * From Table1 
Where columnD=(Select ColumnD From Table1 Where ColumnA=ActiveCell)

1 个答案:

答案 0 :(得分:1)

我已将代码修改为现在按照您的要求进行操作。动画gif显示它测试了3个案例。

enter image description here

Option Explicit
Sub test3()
Dim tableR As Range, cell As Range, r As Range
Dim s As String
Set tableR = Range("A1:C10")
Set r = Selection
For Each cell In tableR
  If cell = r Then
  s = s & cell.Row & ":" & cell.Row & ", "
  End If
Next cell
s = Left(s, Len(s) - 2)
Range(s).Select
End Sub