Separating Sheet into different sheets based on cell "Containing" string in another sheet...

rjbur

New Member
Joined
Nov 8, 2017
Messages
16
Here is my challenge and I am continuing to search the forum for an example, so if there is another useful thread, please point me in that direction.

I have a spreadsheet/workbook that contains two (2) sheets. Lets call them "CRITERIA" and "DATA". I need to perpetrate the DATA into NEW sheets based on the CRITERIA and if the sheet does not exist, create it based on the Header Row in the CRITERIA sheet. If it does exist, then simply add the data to the end of the existing content in the sheet.

DATA contains a long list of... you guessed it, data. Column-A contains "text" or a string. For my example, anything past Column A is along for the ride.

Here is what the CRITERIA sheet might look like:

AB
1CARSCOLOR
2DodgeRed
3ChevyGreen
4GMCBlue

<colgroup><col><col span="2"></colgroup><tbody>
</tbody>

Now the DATA sheet contains say something like:

ABC
1Theredrugwill…Row 1 dataMore Row 1 data
2Chevycarsare…Row 2 dataMore Row 2 data
3Theskyisblue…Row 3 dataMore Row 3 data
4GMCTruckare…Row 4 dataMore Row 4 data
5DodgePowerWagon…Row 5 dataMore Row 5 data
6Thegrassisgreeneron…Row 6 dataMore Row 6 data

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>

Note that I have purposely removed the spacing as the Column-A needs to "contain" the letters and not match a word. As long as somewhere in the cell it has the combination of characters, it contains it and the complete ROW needs to be MOVED to another sheet. If nothing matches, then it is not processed and stays in the current sheet.

In the CRITERIA sheet you can see there are now two columns with the headers of CARS and COLOR. When macro finishes I should have two new sheets, one CARS and the other COLOR.

The other twist is that should I add a third column to CRITERIA it should be processed as well. So we have two open dimensions, the size of the elements under each header and the number of headers to be processed against the DATA.

I did not add a no-match row so that it would stay in the DATA sheet...

I have to run, so I will read through this later today to make sure it makes sense.

Thanks for your time,

Rich
 
If you don't want the blank line, then you can delete it. Add this line of code:
Code:
Sheets(category.Value).Rows(1).EntireRow.Delete
below this line of code
Code:
Next val
(2 occurrences)
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I'm working through the code and stepping slowly through it as I have set a number of variables to be "watched" an noted a strange behavior... ok so it seems strange to me...

The portion of code where the "For" loop starts looks like:

Code:
For Each category In Sheets("Criteria").Range(Cells(1, 1), Cells(1, lColumn1))    
        Set ws = Nothing     
        On Error Resume Next
        
        Set ws = Worksheets(category.Value)
        
        On Error GoTo 0
        
        
        If ws Is Nothing Then.........


So as I watch the variables and I do see that Category.Value does get set prior to the line:

Code:
        Set ws = Worksheets(category.Value)

However, the value of "ws" remains set to "Nothing" as it was in the line:

Code:
        Set ws = Nothing

So when it gets down to the "IF" statement, "ws" is still nothing??

Code:
  If ws Is Nothing Then

Why does "ws" not get assigned as I would have expected?

Thanks,

Rich
 
Last edited:
Upvote 0
At the beginning of the loop, ws is set to nothing which refreshes the variable. The name of ws is then set to the value of 'category'. The first part of the 'If...Else' loop, tests to see if the worksheet with that category name exists. Even though ws is set to category.value, that sheet doesn't exist so ws is therefore still nothing and the sheet is created and the data is copied to that sheet. If the sheet already exists, the loop skips to the 'Else' part and just copies and pastes the data to the already existing sheet. I hope this makes sense.
 
Upvote 0
At the beginning of the loop, ws is set to nothing which refreshes the variable. The name of ws is then set to the value of 'category'. The first part of the 'If...Else' loop, tests to see if the worksheet with that category name exists. Even though ws is set to category.value, that sheet doesn't exist so ws is therefore still nothing and the sheet is created and the data is copied to that sheet. If the sheet already exists, the loop skips to the 'Else' part and just copies and pastes the data to the already existing sheet. I hope this makes sense.

Thanks, makes perfect sense. I'm going to continue to study and learn and thank you again for your time and patience as I wrap my mind around how Excel's macro language works... I'm going to add this explanation into the comments and continue to dive deeper later today into the code...

Rich
 
Upvote 0
It was a pleasure and good luck with it. (y)
 
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,408
Members
449,448
Latest member
Andrew Slatter

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