cdrobinson83
New Member
- Joined
- May 3, 2021
- Messages
- 37
- Office Version
- 365
- Platform
- Windows
Hello,
I'd like to add a step to export worksheets as separate workbooks in a folder to the below macro. Important to note that I only want to export the worksheets that are created as part of the macro. The "Sheet1" I do NOT want to export. Can anyone please help with this?
I'd like to add a step to export worksheets as separate workbooks in a folder to the below macro. Important to note that I only want to export the worksheets that are created as part of the macro. The "Sheet1" I do NOT want to export. Can anyone please help with this?
VBA Code:
Sub B_CREATE()
Dim a, i&, ii&, s$, r As Range, c As Range, dic As Object
Application.ScreenUpdating = False
Set dic = CreateObject("Scripting.Dictionary")
Set r = Sheets("sheet1").[a1].CurrentRegion '<--- Change sheet1 to actual source sheet name
Set c = r.Offset(, r.Columns.Count + 2).Range("a1:a2")
a = Application.Index(r, Application.Sequence(r.Rows.Count, , 1, 1), [{8,4,2,12}])
For i = 2 To UBound(a, 1)
s = Join(Array(a(i, 1), a(i, 2), a(i, 3), a(i, 4)), "_")
If Not dic.exists(s) Then
dic(s) = Empty
If Not Evaluate("isref('" & s & "'!a1)") Then
Sheets.Add(, Sheets(Sheets.Count)).Name = s
End If
For ii = 1 To UBound(a, 2)
If TypeName(a(i, ii)) = "String" Then a(i, ii) = Chr(34) & a(i, ii) & Chr(34)
Next
With Sheets(s)
.UsedRange.CLEAR
r.Rows(1).Copy .[a1]
For ii = 1 To r.Columns.Count
.Columns(ii).ColumnWidth = r.Columns(ii).ColumnWidth
Next
c(2).Formula = "=and(h2=" & a(i, 1) & ",d2=" & a(i, 2) & ",b2=" & a(i, 3) & ",l2=" & a(i, 4) & ")"
r.AdvancedFilter 2, c, .[a1].CurrentRegion
With .Range("a" & Rows.Count).End(xlUp)(2, r.Columns.Count - 1).Resize(, 2)
.FormulaR1C1 = Array("Total", "=sum(r2c:r[-1]c)")
.Font.Bold = True
.Borders.Weight = 2
.Borders.ColorIndex = 15
End With
End With
End If
Next
c.CLEAR
Application.ScreenUpdating = True
End Sub