How to check first character in a cell value

后端 未结 3 741
一向
一向 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:55
    IF(logical_test, [value_if_true], [value_if_false])
    

    Using the above you can do the following:

    IF(LEFT(A1,1)="0","0",IF(LEFT(A1,1)="1","1","Neither"))
    

    Replace A1 with the cell you want to test

    Replace the "0" between the commas with whatever you want to do with those values that start with 0

    Replace the "1" between the commas with whatever you want to do with those values that start with 1

    Replace "Neither" with whatever you want to do with those values that don't start with 0 or 1

    0 讨论(0)
  • 2021-01-29 06:57

    Try:

    Option Explicit
    Sub FirstChar()
        Dim xlString As String
        Dim xlFirstChar As String
    
        xlString = "01999102477490"
    
        xlFirstChar = Left$(xlString, 1)
    
        MsgBox xlFirstChar
    End Sub
    

    String Manipulation

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题