VBA - Identifying alpha characters in CSV to import into arrays

JakeB8

New Member
Joined
Feb 7, 2019
Messages
11
I'm editing VBA code that was created by someone else several years ago. The end goal is to import data from a CSV file into multiple arrays using multiple VBA subs. The main driver for what to import is in column A of the CSV, which is labeled as "ID." There is one sub that searches the ID values for anything beginning with "L" or "H." That part works fine. There is another sub that searches the ID values for anything else, but if the values aren't strictly numerical then they aren't being recognized. We'd like to be able to have alpha-numeric values here, but right now we can't. Generally the alpha characters we need to recognize (other than L or H) would be at the end of the numeric string, but the numeric string isn't always the same length (ex. 1A, 11A, 111A). How can we guarantee the code would recognize the the alpha-numeric strings that don't start with L or H?
ID
PART​
LENGTH
result​
1AA12B20-690--> Is not recognized.
2A12B20-61755--> Recognized and sent to Array 1.
3A12B20-690
4A12B20-0115
5A12B20-01730
6A12B20-0115
L1192-C25241590--> Recognized and sent to Array 2.
H1A71A46-61050--> Recognized and sent to Array 3.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Could you not just see whether the last character is numeric or not and then do something (or not) as needed?
Some examples...

VBA Code:
Sub Test()

    Dim mystring As String
    
    mystring = "111"
    
    ' Use IsNumeric to check whether the whole string is a number or not.
    If IsNumeric(mystring) = True Then
        MsgBox "Just numbers here: " & mystring, vbOKOnly
        ' Do your thing here
    End If
    
    mystring = "1A1B"
    
    ' Same again. IsNumeric to check whether the whole string is a number or not.
    If IsNumeric(mystring) = False Then
        MsgBox "Numbers and letters here: " & mystring, vbOKOnly
        ' Do your thing here
    End If
    
    mystring = "111B"
    
    ' Now just look at the right hand character and do something based on that.
    If IsNumeric(Right(mystring, 1)) = False Then
        MsgBox "ends with a letter: " & mystring, vbOKOnly
        ' Do your thing here
    End If

End Sub
 
Upvote 0
How can we guarantee the code would recognize the the alpha-numeric strings that don't start with L or H?
Use Like operator e.g.
Rich (BB code):
If Your Data Like "[!HL]*" Then
 
Upvote 0

Forum statistics

Threads
1,225,363
Messages
6,184,518
Members
453,238
Latest member
visuvisu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top