VBA Help (Again)

markster

Well-known Member
Joined
May 23, 2002
Messages
579
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello

I just need some macro code to reconfigure reference numbers so I can sort in a pivot table. I have uploaded an image whereby you will see that the reference number in the current format is in COLUMN K from Row 7 downwards. I need it to reconfigure and populate the new format in COLUMN S from Row 7 downwards. If the reference number begins with CL I need it to extracts G number the S number and the J number and add the CL number to the end of the reference number (as shown in the image). If the number begins with TH it just extracts the numbers G number the S number and the J number. I think it probably sounds more complicated than it is but if you look at the image I think it is pretty straightforward for someone who knows how to code VBA.

1623592972474.png


Thanks in advance for any help.
Mark
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try:
VBA Code:
Sub ExtractNumbers()
    Application.ScreenUpdating = False
    Dim rng As Range, vRng As Variant
    For Each rng In Range("K7", Range("K" & Rows.Count).End(xlUp))
        vRng = Split(rng, "_")
        If Left(vRng(0), 2) = "CL" Then
            Range("S" & rng.Row) = Mid(vRng(1), 2, 999) & "_" & Mid(vRng(2), 2, 999) & "_" & Mid(vRng(3), 2, 999) & "_" & Split(vRng(0), ".")(0)
        ElseIf Left(vRng(0), 2) = "TH" Then
            Range("S" & rng.Row) = Mid(vRng(1), 2, 999) & "_" & Mid(vRng(2), 2, 999) & "_" & "_" & Mid(vRng(3), 2, 999)
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
How about this:

VBA Code:
Sub ChangeString()
    Dim str As String, lst As String, i As Long
    Dim spl1
    For i = 7 To Cells(Rows.Count, "K").End(xlUp).Row
        str = Range("K" & i)
        spl1 = Split(str, "_")
        If Left(spl1(0), 1) = "C" Then
            lst = Mid(spl1(1), 2) & "_" & Mid(spl1(2), 2) & "_" & Mid(spl1(3), 2) & "_" & Left(spl1(0), 3)
        End If
        If Left(spl1(0), 1) = "T" Then
            lst = Mid(spl1(1), 2) & "_" & Mid(spl1(2), 2) & "_" & Mid(spl1(3), 2)
        End If
        Range("S" & i) = lst
    Next
End Sub
 
Upvote 0
Solution
Thanks guys will give them a try tomorrow morning and will let you know. Have a great evening.

Thanks again.
Mark
 
Upvote 0
@mumps - Just as a friendly FYI, you have a two underscores here:

VBA Code:
ElseIf Left(vRng(0), 2) = "TH" Then
    Range("S" & rng.Row) = Mid(vRng(1), 2, 999) & "_" & Mid(vRng(2), 2, 999) & "_" & "_" & Mid(vRng(3), 2, 999)
End If

?

igold
 
Upvote 0
@igold
Thanks for picking up on that. There should only be one underscore.
VBA Code:
Range("S" & rng.Row) = Mid(vRng(1), 2, 999) & "_" & Mid(vRng(2), 2, 999)  & "_" & Mid(vRng(3), 2, 999)
 
Upvote 0
Both work a treat - thanks very much guys.
Mark
 
Upvote 0
Both @mumps and my code are going to run essentially at about the same speed. If you had a substantial amount of data this code would run much much quicker than what we initially posted.

VBA Code:
Sub ChangeString()
    Dim spl1, arr, arrs, i As Long
    arr = Range("K7:K" & Cells(Rows.Count, "K").End(xlUp).Row)
    ReDim arrs(1 To UBound(arr), 1 To 1)
    For i = 1 To UBound(arr)
        Select Case Left(arr(i, 1), 1)
            Case Is = "C"
                spl1 = Split(arr(i, 1), "_")
                arrs(i, 1) = Mid(spl1(1), 2) & "_" & Mid(spl1(2), 2) & "_" & Mid(spl1(3), 2) & "_" & Left(spl1(0), 3)
            Case Is = "T"
                spl1 = Split(arr(i, 1), "_")
                arrs(i, 1) = Mid(spl1(1), 2) & "_" & Mid(spl1(2), 2) & "_" & Mid(spl1(3), 2) & "_" & Left(spl1(0), 3)
        End Select
    Next
    Range("S7").Resize(UBound(arrs)) = arrs
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,784
Members
449,124
Latest member
shreyash11

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