MACRO help please

STIRRELL

Board Regular
Joined
Dec 30, 2010
Messages
62
Office Version
  1. 365
Hi, I am trying to modify a macro to filter copy and paste data from one spreadsheet to another based on 2 criteria. I found some code but I am having a little bit of trouble modifying. I know right now this is only filtering on the company and I need help adding the filter on column AB.
I believe the macro below creates a new spreadsheet based on the company number.
Basic information
the spreadsheet name is Certify and the range to copy is columns A to Z
We need to filter on the company code in column F ( for example : 20)
and " X "on column AB


when I try to run the macro it is stopping after the next r "currentWS.UsedRange.AutoFilter"

VBA Code:
Sub FilterThenCopy()
   Dim ws, newWS, currentWS As Worksheet
   targetCol = 6   'define which column you want to break
   Dim objDict As Variant
   Set objDict = CreateObject("Scripting.Dictionary")
   Set currentWS = ActiveSheet
   'Add unique value in targetCol to the dictionary
   Application.DisplayAlerts = False
   For r = 2 To Cells(Rows.Count, targetCol).End(xlUp).Row
     If Not objDict.exists(Cells(r, targetCol).Value) Then
       objDict.Add Cells(r, targetCol).Value, Cells(r, targetCol).Value
     End If
   Next r

  If currentWS.AutoFilterMode = True Then
     currentWS.UsedRange.AutoFilter
  End If
  currentWS.UsedRange.AutoFilter
  For Each k In objDict.Keys
    currentWS.UsedRange.AutoFilter field:=targetCol, Criteria1:=objDict.Item(k)
   'delete worksheet if worksheet of item(k) exist
    For Each ws In ActiveWorkbook.Worksheets
      If wsExists(objDict.Item(k)) Then
        Sheets(objDict.Item(k)).Delete
      End If
    Next ws
   'crate worksheet using item(k) name
    Set newWS = ThisWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count))
    newWS.Name = objDict.Item(k)
    'copy filtered contents to new worksheet
    currentWS.UsedRange.SpecialCells(xlCellTypeVisible).Copy
    newWS.Range("A1:Z50000").Select
    newWS.Paste
  Next k
  currentWS.Activate
  currentWS.AutoFilterMode = False
  Application.DisplayAlerts = True
End Sub

Function wsExists(wksName As String) As Boolean
   On Error Resume Next
   wsExists = CBool(Len(Worksheets(wksName).Name) > 0)
   On Error GoTo 0
End Function
 
Last edited by a moderator:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try this:

VBA Code:
Sub create_worksheets()
  Dim sh As Worksheet
  Dim c As Range
  Dim dic As Object, ky As Variant
  Dim lr As Long
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Set dic = CreateObject("scripting.dictionary")
  Set sh = Sheets("Certify")
  If sh.AutoFilterMode Then sh.AutoFilterMode = False
  
  lr = sh.Range("F" & Rows.Count).End(3).Row
  For Each c In sh.Range("F2:F" & lr)
    dic(c.Value) = Empty
  Next c
  
  For Each ky In dic.Keys
    On Error Resume Next
      Sheets(ky).Delete
    On Error GoTo 0
    With sh.Range("A1:AB" & lr)
      .AutoFilter Range("F1").Column, ky
      .AutoFilter Range("AB1").Column, "X"
      Sheets.Add(, Sheets(Sheets.Count)).Name = ky
      sh.AutoFilter.Range.Range("A1:Z" & lr).Copy Range("A1")
    End With
  Next ky
  
  sh.Select
  sh.ShowAllData
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,527
Messages
6,120,054
Members
448,940
Latest member
mdusw

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