Copy visible cells after autofilter

busby66

New Member
Joined
Sep 30, 2018
Messages
4
Hi Everyone,

I'm a newbie to the forum & only a beginner in VBA trying to self teach myself as I did with MS Excel, so would be grateful for your assistance & advice.

So here's my problem...
I've got a large data set table of information with various row records identified by a unique reference number. I've coded the module to insert a sheet & insert a pivot table on this reference number, so I only see the ref number once. The code then looks at the first number in the pivot which then becomes a variable (myvar) which has been declared as a variant. The next piece of code inserts a new sheet, renames it as myvar then goes back to the data table and applies an autofilter on myvar value.

So all fine until this point where I simply want to copy all visible cells when the filter is applied & copy them to the newly added sheet named myvar. :eeek:

My Code reads:

'Apply filter and copy the data range visible to new sheet
With DataRange
.AutoFilter Field:=3, Criteria1:=myvar
.SpecialCells (xlCellTypeVisible)
.Copy Destination:=Sheets(myvar).Range("A1")<---------- Code falls over on this line? (Run-time error 9 Subscript out of range)
End With

'autofit columns
Sheets(myvar).Columns.EntireColumn.AutoFit


'now clear autofilter to show all records
Sheets(wsSource).AutoFilterMode = False
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Where are you defining a sheet name to the variable myvar? That error normally means either a workbook name or a sheet name doesn't match what is in the code.
 
Last edited:
Upvote 0
This is the previous piece of code that runs the pivot, picks the first ref number, adds the sheet & renames it as myvar.value

'call pivot table sub
Call PivotTable1


'make sure the pivot sheet is active
Sheets("MyPivot").Activate


'select start of first record in the pivot table
Range("A2").Select


'start loop condition
Do While IsNumeric(ActiveCell.Value)


'set 1st ID variable
myvar = ActiveCell.Value


Worksheets.Add
ActiveSheet.Name = myvar
 
Upvote 0
Try
Code:
With DataRange
   .AutoFilter Field:=3, Criteria1:=MyVar
   .SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets(CStr(MyVar)).Range("A1")
End With
 
Upvote 0
That works!

It falls over on the next line of code ....
'autofit columns
Sheets(MyVar).Columns.EntireColumn.AutoFit

Which I rewrote as...
Sheets(CStr(MyVar)).Columns.EntireColumn.AutoFit which seems to work fine.

Many thanks for your help. Can you tell me what I was ding wrong?
 
Upvote 0
Because your sheet names are numbers, if "MyVar" was 10 Sheets(MyVar) is the same as Sheets(10), so VBA is trying to use the sheet index, not the sheet name.
By converting MyVar to a string, you are then using sheets("10") which is the sheet name.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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