VBA Code to Add Worksheet to Workbooks that Split

kamccar

New Member
Joined
Jul 23, 2019
Messages
18
Hi!

I have a code that splits a workbook into multiple workbooks based on unique values in the first column found in Sheet1, however I just want it to also pull the second sheet (Sheet2) that is within the original workbook into all of the workbooks that split.

Any help would be greatly appreciated, the code can be found below. Thank you! :
Sub CreateWorkbooks()

Application.ScreenUpdating = False
Dim LastRow As Long, super As Range, RngList As Object, item As Variant, srcWB As Workbook, srcWS As Worksheet
Set srcWB = ThisWorkbook
Set srcWS = srcWB.Sheets("Sheet1")
LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set RngList = CreateObject("Scripting.Dictionary")
With srcWS
For Each Rng In .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
If Not RngList.Exists(Rng.Value) Then
RngList.Add Rng.Value, Nothing
End If
Next
End With
For Each item In RngList
srcWS.Copy
With Cells(1).CurrentRegion
.AutoFilter 1, "<>" & item
ActiveSheet.AutoFilter.Range.Offset(1, 0).EntireRow.Delete
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
ActiveWorkbook.SaveAs Filename:=srcWB.Path & Application.PathSeparator & item & ".xlsx", FileFormat:=51
ActiveWorkbook.Close False
End With
Next item
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
How about
Code:
   For Each item In RngList
      Sheets(Array(srcWS, "Sheet2")).Copy
      With Sheets("Sheet1")
         .Cells(1).CurrentRegion.AutoFilter 1, "<>" & item
         .AutoFilter.Range.Offset(1, 0).EntireRow.Delete
         If .AutoFilterMode Then .AutoFilterMode = False
         ActiveWorkbook.SaveAs FileName:=srcWB.Path & Application.PathSeparator & item & ".xlsx", FileFormat:=51
         ActiveWorkbook.Close False
      End With
   Next item
 
Upvote 0
Which line gave the error?
 
Upvote 0
Oops, it should be
Code:
[COLOR=#333333]     Sheets(Array(srcWS[/COLOR][COLOR=#ff0000].Name[/COLOR][COLOR=#333333], "Sheet2")).Copy[/COLOR]
 
Upvote 0
I put that in, now where it says "For Each item In RngList" it highlights the "For Each Item" as a compile error and says "for control variable already in use"
 
Upvote 0
You need to replace this part of your code
Code:
[COLOR=#333333]For Each item In RngList[/COLOR]
[COLOR=#333333]srcWS.Copy[/COLOR]
[COLOR=#333333]With Cells(1).CurrentRegion[/COLOR]
[COLOR=#333333].AutoFilter 1, "<>" & item[/COLOR]
[COLOR=#333333]ActiveSheet.AutoFilter.Range.Offset(1, 0).EntireRow.Delete[/COLOR]
[COLOR=#333333]If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False[/COLOR]
[COLOR=#333333]ActiveWorkbook.SaveAs Filename:=srcWB.Path & Application.PathSeparator & item & ".xlsx", FileFormat:=51[/COLOR]
[COLOR=#333333]ActiveWorkbook.Close False[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333]Next item[/COLOR]
with the code I supplied.
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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