我想在vba中编写一个宏来选择所有行,其中B列中的值包含字符'via'。这是我第一次尝试宏而不太确定如何开始。
答案 0 :(得分:1)
这个应该为你做(它对我有用) - 代码改编自here。
Option Explicit
Sub SelectByValue(Rng1 As Range, Value As String)
Dim MyRange As Range
Dim Cell As Object
'Check every cell in the range for matching criteria.
For Each Cell In Rng1
If InStr(1, Cell.Text, "via") Then
If MyRange Is Nothing Then
Set MyRange = Range(Cell.Address)
Else
Set MyRange = Union(MyRange, Range(Cell.Address))
End If
End If
Next
'Select the new range of only matching criteria
MyRange.Select
End Sub
Sub CallSelectByValue()
'Call the macro and pass all the required variables to it.
'In the line below, change the Range and the Value as needed
Call SelectByValue(Range("B1:B10"), "via")
End Sub
如何使用?
如何测试代码?
在运行宏之前:
运行宏后: