1

I am trying parse a number string and create rows accordingly. On the left of the Example Data picture is an example of the input data with the right being my desired output. I am wanting to insert a unique row of data for each digit within the brackets for each number combination.

Example Data

Here is an example of the code I used to try to solve the problem.

Option Explicit

Sub example()


Dim num As Variant
Dim x As Variant
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim m As Integer
Dim test As Variant
Dim test2 As Variant
Dim count As Integer

m = 0

For i = 1 To 3

num = Range("C" & 5 + i + m).Value

For j = 1 To Len(num)

    test = Mid(num, j)

    If Left(Mid(num, j), 1) = "[" Then

        For k = 1 To Len(num) - (j + 1)

            m = m + 1

            Range("C" & 5 + m + i - 1).EntireRow.Insert

            test2 = Left(Mid(num, j + k), 1)

            Range("C" & 5 + m + i - 1).Value = Left(num, j - 1) + test2

        Next k

    End If

Next j

Next i

End Sub
2
  • Also, for the purpose of my example I have the output data starting in column G. For my data I actually want to insert the output in place of where the existing input data was while inserting rows accordingly. This is because within my input data there will be number combinations that do not need to be manipulated interspersed within my data. Commented Jun 17, 2016 at 19:54
  • My code example is assuming the data is in the C column Commented Jun 20, 2016 at 13:23

1 Answer 1

1

Please consider using the following script:

Sub splitcombinations()

Dim rngCell As Range
Set rngCell = ThisWorkbook.Sheets(1).Range("A2")

Dim strCombinationDigits As String, strBaseDigits As String
Dim intCombinationDigitsLen As Integer

Dim x As Integer

Do While rngCell.Value2 <> ""

    If InStr(rngCell.Value2, "[") > 0 Then

        strCombinationDigits = Mid(rngCell.Value2, InStr(rngCell.Value2, "[") + 1, InStr(rngCell.Value2, "]") - InStr(rngCell.Value2, "[") - 1)
        intCombinationDigitsLen = Len(strCombinationDigits)

        strBaseDigits = Left(rngCell.Value2, InStr(rngCell.Value2, "[") - 1)

        ActiveSheet.Range(rngCell.Offset(1, 0), rngCell.Offset(intCombinationDigitsLen - 1, 0)).EntireRow.Insert

        For x = 1 To intCombinationDigitsLen

            rngCell.Offset(x - 1, 0).Value2 = strBaseDigits & Mid(strCombinationDigits, x, 1)
            rngCell.Offset(x - 1, 1).Value2 = rngCell.Offset(0, 1).Value2
            rngCell.Offset(x - 1, 2).Value2 = rngCell.Offset(0, 2).Value2

        Next

    End If

    Set rngCell = rngCell.Offset(intCombinationDigitsLen , 0)

Loop

End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

Actually what you are suggesting assumes a set number of characters within the brackets (7). I actually calculate how many digits with intCombinationDigitsLen and modify how many rows I insert accordingly. Then the reason why I only offset by 1 row each loop is because then I'm checking on every iteration if rngCell contains an opening bracket, assuming if it doesn't (and not blank) that it's an iteration that the script added. It then moves down again. I guess I could offset by intCombinationDigitsLen but wanted to make sure I wasn't missing anything.
Nice code! Just change Set rngCell = rngCell.Offset(1, 0) to Set rngCell = rngCell.Offset(intCombinationDigitsLen, 0)
I see. Okay, got it!
Ahh, I was just responding to your previous post. Yes, that would remove a bunch of needless iterations. I will edit accordingly.
I have tried your script and it looks to be working well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.