Generate subset table from Master table

Nick30075

New Member
Joined
May 16, 2013
Messages
26
I have a table that looks like this:
Need Col D values that have ";" present to be parsed out to create and new worksheet with the results looking like the output below.

I've looked & tried but is beyond my level at this time. Any & all help is greatly appreciated!
:banghead:


Excel 2010
ABCD
1IDSystem NameNamePredecessors
21Sys1Task10
32Sys2Task21
43Sys3Task32
54Sys4Task45
65Sys5Task50
76Sys6Task60
87Sys7Task75
98Sys8Task86
1028Sys9Task923;27;8
117Sys10Task1027
1223Sys11Task110
1327Sys12Task120
Sheet4


Need output like this in a new worksheet:

Excel 2010
ABCD
1New sheet results
2IDSystem NameNamePredecessors
328Sys9Task923;27;8
423Sys11Task110
527Sys12Task120
68Sys8Task86
Sheet1
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Code:
Sub myParentMacro()
     dataSheet = "Sheet1"
     outputSheet = createNewSheet
     columnSearch = "D"
     dataFirstRow = 2
     dataLastRow = Sheets(dataSheet).Range(columnSearch & Rows.Count).End(xlUp).Row
     dataHeaders = 1
     outputHeaders = 2
     outputNextRow = 3
     Call insertHeaders(dataSheet, outputSheet, dataHeaders, outputHeaders)
     Call checkEveryRow(dataSheet, outputSheet, columnSearch, dataFirstRow, dataLastRow)
End Sub

Function createNewSheet()
     Set WS = Sheets.Add
     createNewSheet = WS.Name
End Function

Sub insertHeaders(dataSheet, outputSheet, dataHeaders, outputHeaders)
     Sheets(outputSheet).Range("A1").Value = "New Sheet Results"
     Sheets(dataSheet).Rows(dataHeaders).Copy
     ActiveSheet.Paste Destination:= Sheets(outputSheet).Rows(outputHeaders)
End Sub

Sub checkEveryRow(dataSheet, outputSheet, columnSearch, firstRow, lastRow, pasteRow)
     r = firstRow
     Do Until r > lastRow
          myValue = Sheets(dataSheet).Range(columnSearch & r).Value
          TrueOrFalse = doesValueHaveSemicolon(myValue)
          If TrueOrFalse = True Then
               Call CopyPaste(dataSheet, outputSheet, r, pasteRow)
               pasteRow = pasteRow + 1
          End If
          r = r + 1
     Loop
End Sub

Function doesValueHaveSemicolon(myValue)
     test = InStr(1, myValue, ";")
     If test = 0 Then
          doesValueHaveSemicolon = False
     Else
          doesValueHaveSemicolon = True
     End If
End Function

Sub CopyPaste(dataSheet, outputSheet, copyRow, pasteRow)
     Sheets(dataSheet).Rows(copyRow).Copy
     ActiveSheet.Paste Destination:= Sheets(outputSheet).Rows(pasteRow)
End Sub
 
Last edited:
Upvote 0
Hi WarPigl3t,
the checkEveryRow call is generating a compile error.
The fix is to make the arguments = to the arguments under checkEveryRow sub correct?

Code:
Sub myParentMacro()
     dataSheet = "Sheet1"
     outputSheet = createNewSheet
     columnSearch = "D"
     dataFirstRow = 2
     dataLastRow = Sheets(dataSheet).Range(columnSearch & Rows.Count).End(xlUp).Row
     dataHeaders = 1
     outputHeaders = 2
     outputNextRow = 3
     Call insertHeaders(dataSheet, outputSheet, dataHeaders, outputHeaders)
     [B]Call checkEveryRow(dataSheet, outputSheet, columnSearch, dataFirstRow, dataLastRow[/B])
End Sub

Function createNewSheet()
     Set WS = Sheets.Add
     createNewSheet = WS.Name
End Function

Sub insertHeaders(dataSheet, outputSheet, dataHeaders, outputHeaders)     
Sheets(outputSheet).Range("A1").Value = "New Sheet Results"
     Sheets(dataSheet).Rows(dataHeaders).Copy
     ActiveSheet.Paste Destination:=Sheets(outputSheet).Rows(outputHeaders)
End Sub

[B]Sub checkEveryRow(dataSheet, outputSheet, columnSearch, firstRow, lastRow, pasteRow)[/B]

Thanks!
 
Last edited:
Upvote 0
Made the change noted above and then run into:
Run-time error '1004' Application-defined or object-defined error via the my value assignment bolded below:
Code:
Sub checkEveryRow(dataSheet, outputSheet, columnSearch, firstRow, lastRow, pasteRow)
     r = firstRow
     Do Until r > lastRow
          [B]myValue = Sheets(dataSheet).Range(columnSearch & r).Value[/B]
 
Upvote 0
Code:
Sub myParentMacro()
     dataSheet = "Sheet1"
     outputSheet = createNewSheet
     columnSearch = "D"
     dataFirstRow = 2
     dataLastRow = Sheets(dataSheet).Range(columnSearch & Rows.Count).End(xlUp).Row
     dataHeaders = 1
     outputHeaders = 2
     outputNextRow = 3
     Call insertHeaders(dataSheet, outputSheet, dataHeaders, outputHeaders)
     Call checkEveryRow(dataSheet, outputSheet, columnSearch, dataFirstRow, dataLastRow, outputNextRow)
End Sub

Function createNewSheet()
     Set WS = Sheets.Add
     createNewSheet = WS.Name
End Function

Sub insertHeaders(dataSheet, outputSheet, dataHeaders, outputHeaders)
     Sheets(outputSheet).Range("A1").Value = "New Sheet Results"
     Sheets(dataSheet).Rows(dataHeaders).Copy
     ActiveSheet.Paste Destination:= Sheets(outputSheet).Rows(outputHeaders)
End Sub

Sub checkEveryRow(dataSheet, outputSheet, columnSearch, firstRow, lastRow, pasteRow)
     r = firstRow
     Do Until r > lastRow
          myValue = Sheets(dataSheet).Range(columnSearch & r).Value
          TrueOrFalse = doesValueHaveSemicolon(myValue)
          If TrueOrFalse = True Then
               Call CopyPaste(dataSheet, outputSheet, r, pasteRow)
               pasteRow = pasteRow + 1
          End If
          r = r + 1
     Loop
End Sub

Function doesValueHaveSemicolon(myValue)
     test = InStr(1, myValue, ";")
     If test = 0 Then
          doesValueHaveSemicolon = False
     Else
          doesValueHaveSemicolon = True
     End If
End Function

Sub CopyPaste(dataSheet, outputSheet, copyRow, pasteRow)
     Sheets(dataSheet).Rows(copyRow).Copy
     ActiveSheet.Paste Destination:= Sheets(outputSheet).Rows(pasteRow)
End Sub
 
Upvote 0
Awesome!
That got us the first row written to a new sheet!
Now we need the other values in the semi colon delimited field rows to be copied into the new sheet so it'll look like this:
Almost there!


Excel 2010
ABCD
1IdSystem NameNamePredecessors
228Sys9Task923;27;8
323Sys11Task110
427Sys12Task120
58Sys8Task86
result
 
Upvote 0
From what I can tell, the code does what it is suppose to. Your sheet example of what the new sheet should look like after the macro is not correct. Take row 3 for example. The ID number is 23, System Name is Sys11, Name is Task11, and Predecessors is 0. Let me repeat, Predecessors is 0. It does not contain a semicolon and therefore will not output to the new sheet as were your specifications. Did I misunderstand the requirements?
 
Upvote 0
Oh I get what you're doing now. Predecessors are the ID of the other rows you want copied to the new sheet. This changes everything. Trash the macro I gave you. We need to start from scratch. Will get back with you. Its still an easy macro to build.
 
Upvote 0
Code:
Sub myParentMacro()
     dataSheet = "Sheet1"
     outputSheet = createNewSheet
     columnSearch = "D"
     columnID = "A"
     dataFirstRow = 2
     dataLastRow = Sheets(dataSheet).Range(columnSearch & Rows.Count).End(xlUp).Row
     dataHeaders = 1
     outputHeaders = 2
     outputNextRow = 3
     Call insertHeaders(dataSheet, outputSheet, dataHeaders, outputHeaders)
     Call checkEveryRow(dataSheet, outputSheet, columnSearch, dataFirstRow, dataLastRow, outputNextRow, columnID)
End Sub

Function createNewSheet()
     Set WS = Sheets.Add
     createNewSheet = WS.Name
End Function

Sub insertHeaders(dataSheet, outputSheet, dataHeaders, outputHeaders)
     Sheets(outputSheet).Range("A1").Value = "New Sheet Results"
     Sheets(dataSheet).Rows(dataHeaders).Copy
     ActiveSheet.Paste Destination:= Sheets(outputSheet).Rows(outputHeaders)
End Sub

Sub checkEveryRow(dataSheet, outputSheet, columnSearch, firstRow, lastRow, pasteRow, columnID)
     r = firstRow
     Do Until r > lastRow
          myValue = Sheets(dataSheet).Range(columnSearch & r).Value
          TrueOrFalse = doesValueHaveSemicolon(myValue)
          If TrueOrFalse = True Then
               Call CopyPaste(dataSheet, outputSheet, r, pasteRow)
               pasteRow = pasteRow + 1
               Call pastePredecessorRows(dataSheet, outputSheet, columnSearch, firstRow, lastRow, pasteRow, myValue, columnID)
               pasteRow = Sheets(outputSheet).Range(columnSearch & Rows.Count).End(xlUp).Row + 1
          End If
          r = r + 1
     Loop
End Sub

Function doesValueHaveSemicolon(myValue)
     test = InStr(1, myValue, ";")
     If test = 0 Then
          doesValueHaveSemicolon = False
     Else
          doesValueHaveSemicolon = True
     End If
End Function

Sub CopyPaste(dataSheet, outputSheet, copyRow, pasteRow)
     Sheets(dataSheet).Rows(copyRow).Copy
     ActiveSheet.Paste Destination:= Sheets(outputSheet).Rows(pasteRow)
     Application.CutCopyMode = False
End Sub

Sub pastePredecessorRows(dataSheet, outputSheet, columnSearch, firstRow, lastRow, pasteRow, myValue, columnID)
     mySplit = Split(myValue, ";")
     For Each element in mySplit
          r = firstRow
          myValue = Sheets(dataSheet).Range(columnID & r).Value
          Do Until r > lastRow Or element = myValue
               r = r + 1
               myValue = Sheets(dataSheet).Range(columnID & r).Value
          Loop
          If element = myValue Then
               CopyPaste(dataSheet, outputSheet, r, pasteRow)
               pasteRow = pasteRow + 1
          End If
     Next element
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,277
Messages
6,171,153
Members
452,383
Latest member
woodsfordg

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