VBA Autofilter for Mac to copy rows from one sheet to another

realdemigod

Board Regular
Joined
Aug 19, 2014
Messages
51
Office Version
  1. 365
Platform
  1. MacOS
Hi,
I wasted a lot of time trying to make a VBA code with Autofilter work in Mac but I understand that Excel Mac does not support Autofilter VBA function. Could you anyone suggest an easier alternative?

I have around 8 lakh rows on Sheet1 and I need to copy rows based on a particular cell value present in column 21 from Sheet1 to Sheet2. So I tried with a code which looks like this.

VBA Code:
Sub movedata()

Dim sh As Sheet1
Dim rng As Range

Set rng = sh.Range("A1", sh.Range("A1").End(xlDown)).Select
On Error Resume Next
rng.AutoFilter Field:=21, Criteria1:="UK"
SpecialCells(xlCellTypeVisible).EntireRow.Copy
Sheets(“Sheet2”).Range(“A1”)
.AutoFilter
End With

End Sub

I'm not able to make this work. Pease help, thanks.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Even on a PC, I wouldn't recommend using AutoFilter with code. If you've got formulas in any columns, it can be VERY slow. I prefer the following trick that I learned from @Zack Barresse. I call it the 'Zack method.' It consists of using an additional column to do the required filter logic (eg U2 <> "UK") and using resulting calculation with SpecialCells to select only the rows you want.

So, assuming your data set (with header) is in A1:U20 (I'm hardcoding rows & columns, but you may want to make it more dynamic):
VBA Code:
Sub BetterThanFilter()
Dim rngFormula As Range

With Worksheets("Sheet1")
    Set rngFormula = Range("V2:V20")

    'this will put a 1 if there is a UK, and a #DIV/0! error if not
    rngFormula.FormulaR1C1 = "=1/(RC[-1]=""UK"")"

    'copy the matches or use xlErrors to get the non-matches)
    rngFormula.SpecialCells(xlCellTypeFormulas, xlNumbers).EntireRow.Copy Worksheets("Sheet2").Range("A1")

    'clear formula columns
    rngFormula.Delete (xlShiftToLeft)
    Sheet2.Range("V1").EntireColumn.Delete (xlShiftToLeft)
End With

Set rngFormula = Nothing
End Sub
 
Upvote 0
FWIW, the problem is not a Mac one, it's just that your code is completely wrong. ;) You'd need something like this:

Code:
Sub movedata()

Dim sh As Worksheet
Dim rng As Range
Set sh = Sheet1
Set rng = sh.Range("A1").CurrentRegion
On Error Resume Next
rng.AutoFilter Field:=21, Criteria1:="UK"
rng.SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Sheet2").Range("A1")
rng.AutoFilter
End Sub
 
Upvote 0
@starl - thanks for your help, I'm not good with VBA as you can see so I will try your code later and get back to you as I'm short on time against my deadline as RoryA's code worked easily:biggrin:

@RoryA - thanks for your help it is working :)
 
Upvote 0
@realdemigod - no worries! glad Rory popped in.. I'm just embarrassed by the fact that I didn't read your code more closely and find the issue.
 
Upvote 0
Ha! Yeah, the helper column method shines more in very large data sets as it's so much faster than traditional filtering in those situations. Great tool for the toolbox though.
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,582
Members
449,039
Latest member
Arbind kumar

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