Sheepy1250
New Member
- Joined
- Jun 22, 2020
- Messages
- 6
- Office Version
- 365
- Platform
- Windows
Hi everyone
I'm hoping that someone with far more VBA knowledge than me can help with this one.
I've had some lovely assistance with the original issue but can't quite get to the final result I need.
I have some downloaded data that comes in the following format:
+++TOWN NAME (LLLNN) - where L=Letter and N=Number in a one-off code for each town
I am trying to split the data into adjacent columns so that it looks like this:
TOWN NAME - Column 2
LLLNN - Column 3
The VBA code I have been given by a lovely contributor on (sshhhhhh!) another forum has suggested this:
Which is fantastic but it misses off the final "N" in the column 3 results.
Now, call me Mr Stupid (I guess in VBA terms I am ) but somewhere in that lot is the reason the final number is being trimmed off the 5 character alphanumeric code for the town.
I am guessing this is a really simple fix for anyone with a decent enough working knowledge of VBA, a bunch of people I am definitely not a part of!
Can anyone help please?
Cheers
Mark - a total learner of VBA trying to understand its weirdness
I'm hoping that someone with far more VBA knowledge than me can help with this one.
I've had some lovely assistance with the original issue but can't quite get to the final result I need.
I have some downloaded data that comes in the following format:
+++TOWN NAME (LLLNN) - where L=Letter and N=Number in a one-off code for each town
I am trying to split the data into adjacent columns so that it looks like this:
TOWN NAME - Column 2
LLLNN - Column 3
The VBA code I have been given by a lovely contributor on (sshhhhhh!) another forum has suggested this:
VBA Code:
Option Explicit
Sub SplitData()
Dim r1 As Range, r2 As Range, c As Range
Dim s As String
Dim v As Variant
Set r1 = ActiveSheet.Range("A1")
Set r2 = r1.End(xlDown)
For Each c In Range(r1, r2).Cells
'no spaces
s = Trim(c.Value)
's = "+++TOWN NAME 1 (LLLNN)"
'leading plus's
Do While Left(s, 1) = "+"
s = Right(s, Len(s) - 1)
Loop
's = "TOWN NAME 1 (LLLNN)"
'trailing )'s
Do While Right(s, 1) = ")"
s = Left(s, Len(s) - 1)
Loop
's = "TOWN NAME 1 (LLLNN"
'start at 0, i.e. v(0)
v = Split(s, "(")
'v(0) = "TOWN NAME 1"
'v(1) = "LLLNN"
'*************************************************
'trailing numbers and spaces
Do While Right(v(0), 1) = " " Or IsNumeric(Right(v(0), 1))
v(0) = Left(v(0), Len(v(0)) - 1)
Loop
'v(0) = "TOWN NAME"
'v(1) = "LLLNN"
'put town one col over, and numbers 2 cols over
c.Offset(0, 1).Value = Trim(v(0))
c.Offset(0, 2).Value = Trim(Left(v(1), Len(v(1)) - 1))
Next
End Sub
Which is fantastic but it misses off the final "N" in the column 3 results.
Now, call me Mr Stupid (I guess in VBA terms I am ) but somewhere in that lot is the reason the final number is being trimmed off the 5 character alphanumeric code for the town.
I am guessing this is a really simple fix for anyone with a decent enough working knowledge of VBA, a bunch of people I am definitely not a part of!
Can anyone help please?
Cheers
Mark - a total learner of VBA trying to understand its weirdness
Last edited by a moderator: