Help with Modify VBA

Status
Not open for further replies.

STIRRELL

Board Regular
Joined
Dec 30, 2010
Messages
62
Office Version
  1. 365
Can you please help modify this VBA?
I have a spreadsheet called certify where I need to filter 2 columns( company and payroll batch) then copy and paste columns A to Z onto its own spreadsheet based on company and payroll batch

Company number is in column "F" and Payroll batch "AB"

I believe this macro should filter by the company number and copy and paste onto a new spreadsheet it created based on the company number filtered.

First I have no idea how to add the second filter on column AB the item to be filtered is "X"
second the macro is failing at the next r
Please help

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






currentWS.UsedRange.AutoFilter
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Duplicate to: MACRO help please

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this thread so please continue in the linked thread. If you do not receive a response, you can "bump" it by replying to it yourself, though we advise you to wait 24 hours before doing so, and not to bump a thread more than once a day.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,215,029
Messages
6,122,760
Members
449,095
Latest member
m_smith_solihull

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