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.
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
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.
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