0
votes

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 char "1" )

What I am trying to accomplish is to use an IF statement to distinguish cell values according to "0" and "1" and assign them to different columns. I can probably make the statement on my own, I just need to know how to check the first character for "0" and "1".

I should also say that I have whole column of values to check

3
Possible duplicate of VBA excel - copy cells between workbooksuser4039065

3 Answers

6
votes

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

3
votes
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

3
votes

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.