VBA - Splitting Data in One Cell into Multiple Cells

user13836941

New Member
Joined
Jan 17, 2017
Messages
5
I am trying to split data in one cell into multiple cells and then copy it into a new page. With the code below which I have obtained from Excel VBA- Split Cell Strings into individual cells and copy cells to new sheet - Stack Overflow I can split, copy and paste data for the "Setup" rows and the odd Microphone rows. What I am now having trouble with is splitting and copying the data for all Microphone rows and allocating them to correct "Room".

To my understanding the reason why not all of the Microphone data is being split is because of this line of code
Code:
mic = .Range("B" & i).Offset(2, 0).Value
Is there an alternative to using Offset so I can split all the Microphone rows?

My apologies for the long post, this is my first time posting. Thank you in advance for your help!

Here is what my input data looks like

aIMPq.png


This is what I would like my output to look like
CfYaz.png


This is my code
Code:
Sub Sample()


    Dim myArr, setup, mic
    Dim ws As Worksheet, wsOutput As Worksheet
    Dim Lrow As Long, i As Long, j As Long, rw As Long, col As Long
    Dim arrHeaders, arrHeadersMic


    Set ws = ThisWorkbook.Sheets("Sheet1") '~~> Set this to the relevant worksheet
    With ThisWorkbook
       ' Set wsOutput = .Sheets.Add(after:=.Sheets(.Sheets.Count)) '~~> Add a new worksheet for output
        Set wsOutput = ThisWorkbook.Sheets("Sheet2")
    End With
    
    rw = 3 '<< output starts on this row
    
    arrHeaders = Array("Speaker", "Tables", "People")
    arrHeadersMic = Array("Number", "Manuf", "Model", "ModelNum")
    
        j = 1
    For r = 1 To 1000 ' Do 1000 rows
    
    Select Case Left(Trim(ws.Cells(r, 1).Value), 1000)
    Case "Room 1"
    ws.Rows(r).Copy wsOutput.Rows(j)
    
        With ws
        Lrow = .Range("B" & .Rows.Count).End(xlUp).Row '~~> get the last row
        For i = 1 To Lrow
            If .Cells(i, 1).Value = "Setup" Then
                
                setup = .Range("B" & i).Value
                mic = .Range("B" & i).Offset(2, 0).Value
                
                If Len(setup) > 0 Then


                    myArr = SetupToArray(setup)
                    
                    wsOutput.Cells(rw, 1).Value = "Setup"
                    wsOutput.Cells(rw, 3).Resize(1, 3).Value = arrHeaders 'add the headers
                    wsOutput.Cells(rw, 3).Resize(1, 3).AutoFill _
                       Destination:=wsOutput.Cells(rw, 3).Resize(1, UBound(myArr) + 1) 'fill headers across
                    wsOutput.Cells(rw + 1, 3).Resize(1, UBound(myArr) + 1).Value = myArr 'populate the array
                    
                    
                    wsOutput.Cells(rw + 3, 1).Value = "Microphone"
                    wsOutput.Cells(rw + 3, 3).Resize(1, UBound(arrHeadersMic) + 1).Value = arrHeadersMic
                    
                    If Len(mic) > 0 Then
                        
                        myArr = MicToArray(mic)
                        wsOutput.Cells(rw + 4, 3).Resize(1, UBound(myArr) + 1).Value = myArr
                        
                        
                    End If


                    rw = rw + 6
                End If
            End If
        Next i
    End With


    End Select
    
    
    'j = j + 8


    Next r
    End Sub








    Function SetupToArray(v)
    Dim MYAr, i
    v = Replace(v, ":", ",")
    v = Replace(v, " x ", ",")
    SetupToArray = TrimSpace(Split(v, ","))
    End Function


    Function MicToArray(w)
    w = Replace(w, " x ", " ")
    MicToArray = TrimSpace(Split(w, " "))
    End Function


    Function TrimSpace(arr)
    Dim i As Long
    For i = LBound(arr) To UBound(arr)
        arr(i) = Trim(arr(i))
    Next i
    TrimSpace = arr
    End Function
 

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
user13836941,

Welcome to the MrExcel forum.


You are posting pictures. This means that if this was a problem where one needed to use your data, anyone trying to help you would have to enter the data manually. That makes no sense and I doubt that you would get an answer.


We would like more information. Please see the Forum Use Guidelines in the following link:

http://www.mrexcel.com/forum/board-announcements/127080-guidelines-forum-use.html


See reply #2 at the next link, if you want to show small screenshots, of the raw data, and, what the results should look like.

http://www.mrexcel.com/forum/about-board/508133-attachments.html#post2507729


Or, you can post your workbook/worksheets to the following free site (sensitive data changed), mark the workbook for sharing, and, provide us with a link to your workbook:

https://dropbox.com
 
Upvote 0
Hi hiker95,

Thank you very much for the feedback you have provided me with posting a question. I will be using this site a lot in the future so I would like to my best in posting good questions. From what I have read there is no option to edit my current question? Would you suggest deleting this question and making a new one more suitable to the information you have provided me above?
 
Upvote 0
From what I have read there is no option to edit my current question?

user13836941,

You can add/continue with this thread by clicking on the + Reply to Thread button below (just like you did with your current reply), and, supply screenshots(not PNG displays) that we can use/copy into worksheets, in order to create a macro that will work correctly.

If you are not able to provide actual screenshots, or, your actual workbook/worksheets, per my reply #2, then:

Click on the Reply to Thread button, and just put the word BUMP in the thread. Then, click on the Post Quick Reply button, and someone else will assist you.
 
Last edited:
Upvote 0
There was some useful code in your macro, but something was malformed where you had a loop inside of a Select without a Select Case, so the code would not compile.

The following macro still uses some of the StackOverflow utilities to parse the Setup and Mic information, but uses a new set of logic to look for Rooms, Setup, and Mic rows.
Code:
Sub BuildReport()
    Dim myArr, setup, mic
    Dim ws As Worksheet, wsOutput As Worksheet
    Dim Lrow As Long, i As Long, j As Long, rw As Long, r As Long
    Dim m As Long, MicRow As Long, SetupRow As Long
    Dim arrHeaders, arrHeadersMic

    Set ws = ThisWorkbook.Sheets("Sheet1")
    With ThisWorkbook
        Set wsOutput = ThisWorkbook.Sheets("Sheet2")
    End With
    
    rw = 2 '<< output starts on this row
    
    arrHeaders = Array("Speaker", "Tables", "People")
    arrHeadersMic = Array("Number", "Manuf", "Model", "ModelNum")

    Lrow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row '~~> get the last row
    For i = 1 To Lrow
          If Left(ws.Cells(i, 1).Value, 4) = "Room" Then
          ' Room Info is in Row i. Setup is in Row (i+1).
          wsOutput.Cells(rw, 1).Resize(1, 2).Value = Array(ws.Cells(i, 1).Value, Cells(i, 2).Value)
          rw = rw + 1
          SetupRow = i + 1
          setup = ws.Cells(SetupRow, 2).Value
          If Len(setup) > 0 Then
              myArr = SetupToArray(setup)
              wsOutput.Cells(rw, 1).Value = "Setup"
              wsOutput.Cells(rw, 3).Resize(1, 3).Value = arrHeaders 'add the headers
              wsOutput.Cells(rw, 3).Resize(1, 3).AutoFill _
                 Destination:=wsOutput.Cells(rw, 3).Resize(1, UBound(myArr) + 1) 'fill headers across
              wsOutput.Cells(rw + 1, 3).Resize(1, UBound(myArr) + 1).Value = myArr 'populate the array
              rw = rw + 3
          End If
          
          ' An unknown number of Microphones start in Row (i+2)
          MicRow = SetupRow + 1
          For m = MicRow To (MicRow + 10)
              If ws.Cells(m, 1).Value = "Microphone" Then
                  mic = ws.Cells(m, 2).Value
                  If Len(mic) > 0 Then
                      wsOutput.Cells(rw, 1).Value = "Microphone"
                      wsOutput.Cells(rw, 3).Resize(1, UBound(arrHeadersMic) + 1).Value = arrHeadersMic
                      myArr = MicToArray(mic)
                      wsOutput.Cells(rw + 1, 3).Resize(1, UBound(myArr) + 1).Value = myArr
                      rw = rw + 3
                  End If
              Else
                  Exit For ' reached end of Microphones
              End If
          Next m
      End If
    Next i

End Sub

Function SetupToArray(v)
    Dim MYAr, i
    v = Replace(v, ":", ",")
    v = Replace(v, " x ", ",")
    SetupToArray = TrimSpace(Split(v, ","))
End Function

Function MicToArray(w)
    w = Replace(w, " x ", " ")
    MicToArray = TrimSpace(Split(w, " "))
End Function

Function TrimSpace(arr)
    Dim i As Long
    For i = LBound(arr) To UBound(arr)
        arr(i) = Trim(arr(i))
    Next i
    TrimSpace = arr
End Function
While this code should handle the demo data that you've shown us, it might fail if you have other data that does not follow the pattern of Room, Setup, [Mic 1], [Mic 2], ... [Mic N].
 
Upvote 0
MrExcel and hiker95, Thank you very much for the help and support you have provided. MrExcel thank you so much for the above code it has worked very well for a good amount of workbooks I need to process. I have been able to modify your code to work with other worksheets with different patterns as well which has been great. I am now however a little stuck modifying the code to work with the following sample document:

https://www.dropbox.com/s/l35x5aboqe4av4c/UploadToMrExcel.xlsm?dl=0

I am getting an error on the line
Code:
wsOutput.Cells(rw + 1, 3).Resize(1, UBound(myArr) + 1).Value = myArr 'Run-time error 1004 Application Defined or Object Defined error

I have also commented in my code where I am getting the 'run-time error 1004'
Many thanks yet again!



Code:
Sub BuildReport()    Dim myArr, setup, mic
    Dim ws As Worksheet, wsOutput As Worksheet
    Dim Lrow As Long, i As Long, j As Long, rw As Long, r As Long
    Dim m As Long, MicRow As Long, SetupRow As Long
    Dim arrHeaders, arrHeadersMic


    Set ws = ThisWorkbook.Sheets("Sheet1")
    With ThisWorkbook
        Set wsOutput = ThisWorkbook.Sheets("Sheet2")
    End With
    
    rw = 2 '<< output starts on this row
    
    arrHeaders = Array("Channel", "Number of Modules", "People")
    arrHeadersMic = Array("Number", "Manuf", "Model", "ModelNum")


    Lrow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row '~~> get the last row
    For i = 1 To Lrow
          If Left(ws.Cells(i, 1).Value, 60) = "1. Room  " Then
          ' Room Info is in Row i. Setup is in Row (i+1).
          wsOutput.Cells(rw, 1).Resize(1, 2).Value = Array(ws.Cells(i, 1).Value, Cells(i, 2).Value)
          rw = rw + 1
          
          
          MicRow = i + 1
          For m = MicRow To (MicRow + 10)
            If ws.Cells(m, 1).Value = "Microphone 1*" Then
                myArr = MicToArray(mic)
                mic = ws.Cells(m, 2).Value
                If Len(mic) > 0 Then
                    wsOutput.Cells(rw, 1).Value = "Microphone 1*"
                    wsOutput.Cells(rw, 3).Resize(1, 3).Value = arrHeadersMic ' MAY NEED TO CHANGE .Resize(1, UBound(myArr) + 1).Value=


                    'LINE BELOW GIVES ERROR
                    wsOutput.Cells(rw + 1, 3).Resize(1, UBound(myArr) + 1).Value = myArr 'Run-time error 1004 Application Defined or Object Defined error
                    


                    rw = rw + 3
                End If
            Else
                Exit For 'reched end of Microphones
            End If
          Next m
          
          
          SetupRow = MicRow + 1
          setup = ws.Cells(SetupRow, 2).Value
          If ws.Cells(i, 1).Value = "Setup" Then
          
            If Len(setup) > 0 Then
              myArr = SetupToArray(setup)
              wsOutput.Cells(rw, 1).Value = "Setup"
              wsOutput.Cells(rw, 3).Resize(1, UBound(arrHeaders) + 1).Value = arrHeaders ' MAY NEED TO CHANGE .Resize(1,3).Value =
              wsOutput.Cells(rw, 3).Resize(1, 3).AutoFill _
              Destination:=wsOutput.Cells(rw, 3).Resize(1, UBound(myArr) + 1) ' fil headers acrooss
              wsOutput.Cells(rw + 1, 3).Resize(1, UBound(myArr) + 1).Value = myArr 'populate the array
              rw = rw + 3
            
            End If
          
          End If
          
          
          
          
          
     End If ' If for module area
 Next i
          
          
          
          


End Sub


Function SetupToArray(v)
    Dim MYAr, i
    v = Replace(v, ":", ",")
    v = Replace(v, "|", ",")
    v = Replace(v, " x ", ",")
    SetupToArray = TrimSpace(Split(v, ","))
End Function


Function MicToArray(w)
    w = Replace(w, " x ", " ")
    MicToArray = TrimSpace(Split(w, " "))
End Function


Function TrimSpace(arr)
    Dim i As Long
    For i = LBound(arr) To UBound(arr)
        arr(i) = Trim(arr(i))
    Next i
    TrimSpace = arr
End Function
 
Upvote 0
MrExcel and hiker95, Thank you very much for the help and support you have provided.

user13836941,

Thanks for the feedback.

You are very welcome.

You are in very good hands with MrExcel.
 
Upvote 0
Good lesson for me here... I didn't take the time to figure out what the StackOverflow code was doing before I posted it.
Add an If block to prevent the AutoFill from making copies when it does not need to make copies:

Code:
         If Len(setup) > 0 Then
              myArr = SetupToArray(setup)
              wsOutput.Cells(rw, 1).Value = "Setup"
              wsOutput.Cells(rw, 3).Resize(1, 3).Value = arrHeaders 'add the headers
              [COLOR="#FF0000"]' is there more than one set of headers needed?[/COLOR]
              [COLOR="#FF0000"]If UBound(myArr) > 2 Then[/COLOR]
                wsOutput.Cells(rw, 3).Resize(1, 3).AutoFill _
                   Destination:=wsOutput.Cells(rw, 3).Resize(1, UBound(myArr) + 1) 'fill headers across
              [COLOR="#FF0000"]End If[/COLOR]
              wsOutput.Cells(rw + 1, 3).Resize(1, UBound(myArr) + 1).Value = myArr 'populate the array
              rw = rw + 3
          End If
 
Upvote 0
Hi Mr Excel Thank you providing me with that! Unfortunately even with that block of code I am still having trouble making it robust enough to work with a variety of Workbook .If you could please look at my code in the module of the dropbox link https://www.dropbox.com/s/nyv4akbe2rwrtek/UploadToMrExcel1.xlsm?dl=0 that would be awesome . I am currently trying to get the output desired as shown in Sheet4. That being said I am still trying to copy to Sheet2. From what I have come to realize from my debugging it seems like my "MicRow" is not incrementing correctly, resulting in my "SetUp" to not increment correctly. As a result of this I can not split and copy any of the data for "2.Room Office Area". Any ideas of how to remedy this would be much appreciated! Thank you again!

Also is it bad practice to keep replying with my updated code? I thought it would be better to just provide a link with the file containing the sample data, wanted output and code.
I thank you yet again for your help. This has been troubling me for some time now and it great to have another view point from someone more experienced.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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