VBA How to identify if a column's contents is numerical?

狂风中的少年 提交于 2019-12-06 15:59:48

COUNT counts numbers; COUNTA counts numbers and text.

=Count(A:A)=CountA(A:A)

... returns true if column A contains numbers only. In VBA as,

Debug.Print CBool(Application.Count(Columns(1))=Application.CountA(Columns(1)))

To convert text-that-looks-like-numbers to true numbers, use TextToColumns, Fixed width, Finish on the column.

 Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, FieldInfo:=Array(0, 1)

Probably the simplest solution I can thing of:

Sub TestMe()

    Dim myCell  As Range
    Dim myCol   As Range

    Set myCol = Worksheets(1).Columns(1).Cells.SpecialCells(2)

    For Each myCell In myCol.Cells
        If Not IsNumeric(myCell) Then
            Debug.Print myCell.Address
            Exit For
        End If
    Next myCell

End Sub

As a next step, try writing a boolean function, that takes the worksheet and the column to search for. It can be of use.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!