db702
db702

Reputation: 568

Looping through a column in excel and creating an identical sheet that is filtered for each value found

Imagine I have an master excel sheet in the following format:

store, date, total sales
NY, 1/1, 10
NY, 1/2, 15
WA, 1/1, 12
WA, 1/2, 14

Now, using VBA, I want to create a separate tab for each store, where the tab contains the same columns and all the rows where that store is mentioned. For example, there would be a tab called NY with the following:

store, sate, total sales
NY, 1/1, 10
NY, 1/2, 15

There would also be another tab for WA, and for any other store name found in the master sheet.

Here is the code that I have so far:

Sub SplitandFilterSheet()
'Step 1 - Name your ranges and Copy sheet
'Step 2 - Filter by Department and delete rows not applicable
'Step 3 - Loop until the end of the list

Dim Splitcode As Range
Sheets("Master Sheet").Select
Set Splitcode = Range("Splitcode")

For Each cell In Splitcode
Sheets("Master Sheet").Copy After:=Worksheets(Sheets.Count)
ActiveSheet.Name = cell.Value

With ActiveWorkbook.Sheets(cell.Value).Range("MasterData")
.AutoFilter Field:=1, Criteria1:="<>" & cell.Value, Operator:=xlFilterValues
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With

ActiveSheet.AutoFilter.ShowAllData
Next cell
End Sub

The error I am getting is on the line starting with ".Offset()". The error is the following:

Run-time error '1004':

Application-defined or object-defined error

For context, the master sheet is called 'Master Sheet', all the data in the master table is in a range called 'MasterData', which includes the first row of headers. Finally, there is a list of possible store names stored in the variable 'Splitcode'.

I would rather do this in VBA than in python or R for a number of reasons, but am struggling to understand this error.

Upvotes: 0

Views: 79

Answers (1)

urdearboy
urdearboy

Reputation: 14580

sorry for the code dump. I tried to edit a macro I used to use a work that was very close to your needs but I seem to have missed something along the way

Props for getting it to work yourself though!!

Sub parse_data()

Dim lr As Long, icol As Long, i As Long, vcol As Long, titlerow As Long
Dim ws As Worksheet
Dim myarr As Variant
Dim title As Range

vcol = 1
Set ws = Sheets("Sheet1")
lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row

Set title = ws.Range("A1:C" & lr)
titlerow = title.Cells(1).Row
icol = ws.Columns.Count
ws.Cells(1, icol) = "Unique"

For i = 2 To lr
On Error Resume Next
    If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
        ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
    End If
Next

myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
ws.Columns(icol).Clear

For i = 2 To UBound(myarr)
  title.AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
    If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
        Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = myarr(i) & ""
    Else
        Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.Count)
    End If
  ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A1")
  Sheets(myarr(i) & "").Columns.AutoFit
Next

ws.AutoFilterMode = False
ws.Activate

End Sub

Upvotes: 2

Related Questions