我在一列单元格中有一组参考数据(它可能是一行,但无论如何) .
我想通过几个工作表,检查选定的值范围,如果当前单元格值在参考数据中,则将其用作粘贴到下一系列单元格的偏移量的值,直到当前单元格更改为某些参考数据中的新值,此时粘贴过程应重复新值和下一系列单元格 . 例如
Sub doStuff()
Dim RefRange As Range, queryRange As Range, checkCell As Rnage
String pasteDate As String
Set RefRange = Range("A1:A50")
Set queryRange = .Selected
For Each checkCell In queryRange
If checkCell.Value IN RefRange <***here's where I don't know what to use ***>
pasteData = checkCell.Value
' Advance to next item
Next
ElseIf checkCell.Value Not In queryRange <***here's where I don't know what to use ***>
ActiveCell.Offset(0,+1).Value = pasteData
End If
Next
End Sub
我知道如何在SQL中执行此操作,VBA中是否有类似的方法?
在SQL中:
Select a, b from table where c in ('foo', 'bar', 'baz') OR
Select a, b from table where c in (select e from some_other_table)
TIA
1 回答
我怀疑是否需要遍历所有数据只需在整个列中使用公式并替换为结果值 . 像这样:
上面的代码查看B列中的所有值,以测试A列中的匹配项 . 如果找到匹配,则B列值显示在c列中 . 如果未找到匹配项,则会显示一个空白单元格,但您可以将其更改为您想要的任何内容 .