String, substring

  • Thread starter Thread starter petergr
  • Start date Start date
P

petergr

Hi, I am new to Excel VBA. Please help me with this:
How can I take a part from a string and check if it is letters o
numbers. In my particular case I need to split sea containers numbers
for example HDMU1254324, so the task is to take for first letters
check if the all first letters are really letters and put the prefix o
container in one sell and the number into another cell. Thank you
 
Here is an example

Dim s As String
Dim i As Long
Dim nChar As Long


s = "HDMU1234"

For i = 1 To 4
nChar = Asc(Mid(s, i, 1))
If nChar < Asc("A") Or nChar > Asc("Z") Then
MsgBox "Illegal character " & Mid(s, i, 1)
Exit For
End If
Next

If Not IsNumeric(Mid(s, 5, 99)) Then
MsgBox "Wrong tail " & Mid(s, 5, 99)
End If

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
I think it would be better to use VBA's isnumber() in this case.

application.isnumber will return false when used with the Left function (left
returns a string).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top