I'm using VBA in Excel to try and write some macros, however, I am very new to the process.
At the moment, I am trying to use a for loop to search a column for a non-numeric value. To do this I wrote the following:
rwcnt = WorksheetFunction.CountA(Range("A:A"))
Dim i As Integer
For i = 1 To rwcnt
If Cells(i, 1).Value = Not IsNumeric Then
Cells(i, 1).Select
Range(Selection, Selection.End(xlDown)).Select
Exit For
End If
This is returning an error saying that the argument is not optional and it highlights IsNumeric.
What I'd like to accomplish is to search column A and select the first cell that contains non-numeric characters in it outside of my headers. Also, this is searching through >100K cells so if there is a less intensive way to do this process, suggestions would also be nice.
Any help would be appreciated and again, I don't know much at all about this stuff so if everything is wrong, feel free to say so.
The below code should work fine, note how I have used IsNumeric
Sub t()
rwcnt = WorksheetFunction.CountA(Range("A:A"))
Dim i As Integer
For i = 1 To rwcnt
If Not (IsNumeric(Cells(i, 1).Value)) Then
range(Cells(i, 1).address, Cells(i, 1).End(xlDown).address).Select
Exit For
End If
Next
End Sub
Also you dont need all of them select's, the above achieves the same result
IsNumeric()
is a function to test an expression, so it should be used like this :
rwcnt = WorksheetFunction.CountA(Range("A:A"))
Dim i As Integer
For i = 1 To rwcnt
If IsNumeric(Cells(i, 1).Value) <> True Then
Cells(i, 1).Select
Range(Selection, Selection.End(xlDown)).Select
Exit For
End If
Next i
When in VBA Editor, press F2 to see Object Browser to get info on functions and press F1 to open Help on that specific function! ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With