ukbulldog001
New Member
- Joined
- Jul 8, 2015
- Messages
- 26
- Office Version
- 2016
- Platform
- Windows
VBA Code:
Sub ReArrangeData()
With Range("B1:B" & Range("A" & Rows.Count).End(xlUp).Row)
.FormulaR1C1 = "=ExpandedSeries(RC[-1])"
.Value = .Value
End With
Columns(2).AutoFit
End Sub
Function ExpandedSeries(ByVal S As String) As String
Dim X As Long, Z As Long
Dim Letter As String, NumberLeft As String, NumberRight As String, Parts() As String
S = Replace(Replace(Application.Trim(Replace(S, ",", " ")), " -", "-"), "- ", "-")
Parts = Split(S)
For X = 0 To UBound(Parts)
If Parts(X) Like "*-*" Then
For Z = 1 To InStr(Parts(X), "-") - 1
If IsNumeric(Mid(Parts(X), Z, 1)) Then
Letter = Left(Parts(X), Z + (Left(Parts(X), 1) Like "[A-Za-z]"))
If IsNumeric(Letter) Then Letter = ""
NumberLeft = Mid(Left(Parts(X), InStr(Parts(X), "-") - 1), Z, 99)
NumberRight = Replace(Mid(Parts(X), InStr(Parts(X), "-") + 1), Letter, "")
Exit For
End If
Next
For Z = NumberLeft To NumberRight
ExpandedSeries = ExpandedSeries & ", " & Letter & Z
Next
Else
ExpandedSeries = ExpandedSeries & ", " & Parts(X)
End If
Next
ExpandedSeries = Mid(ExpandedSeries, 3)
End Function
the above code works well if the character are just alphanumeric.
Ex: A1-A5 = A1, A2, A3, A4, A5
BGA00-BGA03 = BGA0, BGA1, BGA2, BGA3
have below two issues...
not considering "C02ENT00-C02ENT02" which should give output as C02ENT00, C02ENT01, C02ENT02
and as stated above "BGA00-BGA03" should give output as BGA00, BGA01, BGA02, BGA03
let me know if this is possible or any workaround.
Thanks.