How to check first character in a cell value

后端 未结 3 744
一向
一向 2021-01-29 06:30

I was wondering if there is a way to check the first character in a cell value.

E.g

01999102477490 ( first char \"0\" )

11003200602650 (first cha         


        
3条回答
  •  面向向阳花
    2021-01-29 06:58

    A slightly different approach.

    Dim a As Long, arr As Variant
    With Worksheets("Sheet1")
        arr = .Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(xlUp)).Value
        For a = LBound(arr, 1) To UBound(arr, 1)
            Select Case Asc(arr(a, 1))  'might have to be .Range("A1").Text
                Case 48
                    Debug.Print "it's a zero"
                    'do something with arr(a, 1)
                Case 49
                    Debug.Print "it's a one"
                    'do something with arr(a, 1)
                Case Else
                    Debug.Print "it's something else"
            End Select
        Next a
    End With
    

    By bulk loading the values in column A to a variant array and cycling through the array, you should same some read-time on the loop.

    If you can work with all of the zeroes at once and then all of the ones at once, consider a AutoFilter method.

提交回复
热议问题