Select columns until find a value
I would like to find the Visual Basic command to select a nonspecific number of columns. For example, I开发者_Python百科 have a list of values in a row and I have to select them from the first column until I'll find a "5". How can I do this?
You can do this quickly find Find, which also enables a "not found" result by testing if rng1 exists post find
to search in row 1 of the activesheet for a variable strfind and higlight from column A to the found value
Sub QuickFind()
Dim rng1 As Range
Dim strFind As String
strFind = "5"
Set rng1 = ActiveSheet.Rows(1).Find(strFind, , xlValues, xlWhole)
If rng1 Is Nothing Then
MsgBox strFind & " not found"
Else
Range(rng1, rng1.End(xlToLeft)).Activate
End If
End Sub
Try this:
Public Function Search(row As Integer) As Integer
Dim col As Integer
col = 1
While ActiveSheet.Cells(row, col) <> "5"
col = col + 1
Wend
Search = col
End Function
精彩评论