VBA how to transform a string of data into something else?

MichaelRSnow

Active Member
Joined
Aug 3, 2010
Messages
409
Hi all, I've googled this but can't find a simple solution to the problem

I'm trying to convert a string that looks like this
X = 7/7/7/7/7/6/5/4/3/2 to X = 607001260012500124001230012200
Simple logic, each step between / is a 12 month step and value is a percentage, where the values are the same combine the months to the one value
5 x 7 would be 5 x 12 = 60, 7 is 7 percent so 700 (without the decimal place)
1 x 6 would be 1 x 12 = 12, 6 is 6 percent so 600 (without the decimal place)

I've see some split logic but struggle to find a way to merge the 5 steps of 7 in 1 step of 60700

Any assistance would be gratefully appreciated
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
This creates the string as 12700127001270012700127001260012500124001230012200
But I would like to get it to 607001260012500124001230012200

VBA Code:
Sub Code20Logic2()
Dim ERCStr As String
Dim x As Variant
Dim i As Long
Dim y As Variant


'Select First Cell
Sheets("Code020").Range("B2").Activate

'when to stop
Do Until ActiveCell.Value = ""

ERCStr = "/" & ActiveCell.Value

    x = Split(ERCStr, "/")
    For i = 1 To UBound(x)
        y = x(i) * 100
        
    ActiveCell.Offset(0, 1).Value = ActiveCell.Offset(0, 1).Value & "12" & y

    Next i

'Go down to next row
ActiveCell.Offset(1, 0).Activate
Loop


End Sub
 
Upvote 0
Does this user-defined function do what you want?

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

VBA Code:
Function StrTransform(s As String) As String
  Dim Bits As Variant
  Dim i As Long, j As Long
 
  Bits = Split(s, "/")
  For i = 1 To UBound(Bits)
    If Bits(i) = Bits(i - 1) Then
      j = j + 1
    Else
      StrTransform = StrTransform & 12 * (j + 1) & Bits(i - 1) * 100
      j = 0
    End If
  Next i
  StrTransform = StrTransform & 12 * (j + 1) & Bits(UBound(Bits)) * 100
End Function

MichaelRSnow.xlsm
AB
17/7/7/7/7/6/5/4/3/2607001260012500124001230012200
27/7/7/7/7/6/6/6/5/5/460700366002450012400
37/7/7/7/7/6/6/6/5/5/5607003660036500
4512500
510/10/10/10/648100012600
Sheet1
Cell Formulas
RangeFormula
B1:B5B1=StrTransform(A1)
 
Upvote 1
Solution
Does this user-defined function do what you want?

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

VBA Code:
Function StrTransform(s As String) As String
  Dim Bits As Variant
  Dim i As Long, j As Long
 
  Bits = Split(s, "/")
  For i = 1 To UBound(Bits)
    If Bits(i) = Bits(i - 1) Then
      j = j + 1
    Else
      StrTransform = StrTransform & 12 * (j + 1) & Bits(i - 1) * 100
      j = 0
    End If
  Next i
  StrTransform = StrTransform & 12 * (j + 1) & Bits(UBound(Bits)) * 100
End Function

MichaelRSnow.xlsm
AB
17/7/7/7/7/6/5/4/3/2607001260012500124001230012200
27/7/7/7/7/6/6/6/5/5/460700366002450012400
37/7/7/7/7/6/6/6/5/5/5607003660036500
4512500
510/10/10/10/648100012600
Sheet1
Cell Formulas
RangeFormula
B1:B5B1=StrTransform(A1)
Thanks for sharing this solution :) , works perfectly and i'll look to update my profile information
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,087
Messages
6,123,050
Members
449,092
Latest member
ikke

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