VBA Split multiple worksheet based on cell value

Edward11

New Member
Joined
Jan 21, 2021
Messages
13
Platform
  1. Windows
Hi guys,

I had on earlier posted a thread on splitting worksheet based on the cell value. That vba has been working perfectly. Refer to link below.

However, I've been exploring to split 2 worksheets based on the similar cell value. I tried to search in the forum but I can't find any.

Refer to the sample file for better illustration.

I will need to split the 2 worksheets, namely "A" and "B" based on the similar cell value in "Bch".

I'm not sure is this feasible, can anyone give a some guides?
Appreciate any inputs. Thanks!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Thanks a lot @mumps @Fluff
It's working perfectly now. I will use this to further enhance according to my needs.
Btw, as this macro will be used by a few of us, is there a way to default the new workbook to be created with 3 sheets? Like using the macro to set it as default or creating 2 worksheets in the new workbook.
 
Upvote 0
As Fluff pointed out, the default setting is to create 3 sheets. You can change the setting in Options, General but this means that the new setting will apply to all workbooks added in the future. Insert this code:
VBA Code:
Application.DisplayAlerts = False
Sheets(3).Delete
Application.DisplayAlerts = True
Directly below this line:
VBA Code:
Workbooks.Add
This will delete the third sheet.
 
Upvote 0
If you want to add 3 sheets regardless of your default setting you can use this mod to mumps' code
VBA Code:
Sub CreateSheets()
    Application.ScreenUpdating = False
    Dim srcWB As Workbook, ws As Worksheet, i As Long, arr As Variant, key As Variant, dic As Object, k As Variant, fnd As Range
    Dim Shts As Long
    
    With Application
      Shts = .SheetsInNewWorkbook
      .SheetsInNewWorkbook = 3
    End With
    
    Set srcWB = ThisWorkbook
    Set dic = CreateObject("Scripting.Dictionary")
    For Each ws In srcWB.Sheets(Array("A", "B"))
        arr = ws.Range("C4", ws.Range("C" & Rows.Count).End(xlUp)).Value
        For i = 1 To UBound(arr, 1)
            If Not dic.Exists(arr(i, 1)) Then
                dic.Add arr(i, 1), Nothing
            End If
        Next i
    Next ws
    For Each k In dic.keys
        Set fnd = srcWB.Sheets("A").Range("C:C").Find(k, LookIn:=xlValues, lookat:=xlWhole)
        If Not fnd Is Nothing Then
            With srcWB.Sheets("A")
                .Range("A3").CurrentRegion.AutoFilter 3, k
                Workbooks.Add
                .AutoFilter.Range.Copy Sheets(1).Range("A1")
                Sheets(1).Columns.AutoFit
                .Range("A3").AutoFilter
            End With
            Set fnd = srcWB.Sheets("B").Range("C:C").Find(k, LookIn:=xlValues, lookat:=xlWhole)
            If Not fnd Is Nothing Then
                With srcWB.Sheets("B")
                    .Range("A3").CurrentRegion.AutoFilter 3, k
                    .AutoFilter.Range.Copy Sheets(2).Range("A1")
                    Sheets(2).Columns.AutoFit
                    .Range("A3").AutoFilter
                End With
            End If
            With ActiveWorkbook
                .SaveAs Filename:=srcWB.Path & "\" & k & ".xlsx"
                .Close False
            End With
        End If
    Next k
    Application.SheetsInNewWorkbook = Shts
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,656
Messages
6,126,055
Members
449,284
Latest member
fULMIEX

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