简体   繁体   English

在VBA Excel中搜索非数字值

[英]Searching for a non-numeric value in VBA Excel

I'm using VBA in Excel to try and write some macros, however, I am very new to the process. 我在Excel中使用VBA尝试编写一些宏,但是,我对这个过程很新。

At the moment, I am trying to use a for loop to search a column for a non-numeric value. 目前,我正在尝试使用for循环来搜索列中的非数字值。 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. 这返回一个错误,说该参数不是可选的,它突出显示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. 我想要完成的是搜索A列并在我的标题之外选择包含非数字字符的第一个单元格。 Also, this is searching through >100K cells so if there is a less intensive way to do this process, suggestions would also be nice. 此外,这是搜索> 100K单元格,所以如果有一个不太密集的方式来完成这个过程,建议也会很好。

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 下面的代码应该可以正常工作,请注意我是如何使用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 : IsNumeric()是一个测试表达式的函数,因此它应该像这样使用:

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! 在VBA编辑器中,按F2查看对象浏览器以获取有关功能的信息,然后按F1打开该特定功能的帮助! ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM