Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for a non-numeric value in VBA Excel

Tags:

excel

vba

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.

like image 670
Scott T Avatar asked Mar 16 '23 08:03

Scott T


2 Answers

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

like image 86
99moorem Avatar answered Mar 30 '23 01:03

99moorem


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! ;)

like image 41
R3uK Avatar answered Mar 30 '23 01:03

R3uK