VBA to split data into new sheets based on TWO columns/criteria?

cdrobinson83

New Member
Joined
May 3, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
I’ve seen code shared that will split data into new sheets within a workbook based on criteria in a column. Is this possible to do with more than one column? For example, if I have an account (columns A) with two different products (column B), can I write to create two new sheets? The workaround I have is to merge the data of the two columns into one and then use that new column as the criteria used to split into each sheet. Is there a way to bypass this step?
 

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.
Like this?
VBA Code:
Sub siplitAccounts()
  Dim lRow As Long
  Dim ws As Worksheet
  lRow = Cells(Rows.Count, 1).End(xlUp).Row
  For ascii = 65 To 66
    Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
    ws.Name = "Copy" & Chr(ascii)
    ws.Cells(1, 1).Value = "Account"
    ws.Cells(1, 2).Value = "Product"
  Next
  For i = 2 To lRow - 1
    For j = i + 1 To lRow
      If Cells(i, 1).Value = Cells(j, 1).Value Then
        With Worksheets("CopyA")
        .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row + 1, 1).Value = Cells(i, 1).Value
        .Cells(.Cells(.Rows.Count, 2).End(xlUp).Row + 1, 2).Value = Cells(i, 2).Value
        End With
        With Worksheets("CopyB")
        .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row + 1, 1).Value = Cells(j, 1).Value
        .Cells(.Cells(.Rows.Count, 2).End(xlUp).Row + 1, 2).Value = Cells(j, 2).Value
        End With
      End If
    Next
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,143
Messages
6,123,280
Members
449,094
Latest member
GoToLeep

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