1004 error .advancedfilter

Youngdand

Board Regular
Joined
Sep 29, 2017
Messages
123
Hi,

I have a sheet, with a dataset which is 18 columns, however, further datasets of 18 columns are repeated throughout the sheet horizontally. I need to stack this data vertically. below is a crude example of the table i have and what i need to do is take all the data outside of columns a and b, and to add them to bottom of column a and b ideally without any spaces.


ABCDEFGHIJ
Set 1Set1Set 2Set 2Set 3Set 3Set 4Set 4Set 5Set 5
acbacbabc1abc1
bcdbcdbcd1bcd1bcd2bcd2
cdecdecde1cde1
defdefdef1def1def2def2def3def3
efgefgefg1efg1
fghfghfgh1fgh1fgh2fgh2fgh3fgh3fgh4fgh4
ghighighi1ghi1
ghighi

<tbody>
</tbody>


















I have been trying to write a macro which achieves this however, i am getting a 1004 error in relation to an advanced filter i am trying to perform.

Code:
Sub Stackrepeats()
Dim lr As Long
Dim lastcol As Long
Dim cr As Range
Dim TS As Worksheet
Dim NLR As Long
Set TS = ActiveSheet

Do Until lastcol = 18
With TS.UsedRange
lr = Cells(Rows.Count, 1).End(xlUp).Row
lastcol = .Columns(.Columns.Count).Column
Set cr = .Range(.Cells(1, 19), .Cells(lr, lastcol))
cr.AdvancedFilter Action:=xlFilterCopy, criteriarange:=TS.Range(.Cells(1, 1)), copytorange:=TS.Range(.Cells(lr + 1, 1)), Unique:=True
cr.ClearContents
Application.CutCopyMode = False
End With
Loop
End Sub

it fails on line:

cr.AdvancedFilter Action:=xlFilterCopy, criteriarange:=TS.Range(.Cells(1, 1)), copytorange:=TS.Range(.Cells(lr + 1, 1)), Unique:=True

Can anyone help on this? is there a better way of achieving what i am after?

Cheers,

Dan.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
HI,

Not that i am aware of, as i thought using this method, i would get only unique values based on the rangecriteria specified.

But that just be my twisted logic!.
 
Upvote 0
So it took a while with my basic knowledge of VBA but eventually i came up with the solution below, if anyone is interested:

Code:
Sub Stackrepeats()
Dim lr As Long
Dim lastcol As Long
Dim cr As Range
Dim TS As Worksheet
Dim NLR As Long
Set TS = ActiveSheet
TS.Range("a1").EntireRow.Insert
Application.ScreenUpdating = False
With TS.UsedRange
lr = TS.Cells(Rows.Count, 1).End(xlUp).Row
lastcol = .Columns(.Columns.Count).Column
Debug.Print (lastcol)
End With
For i = 18 To lastcol
If lastcol = 18 Then GoTo Finish
lr = TS.Cells(Rows.Count, 1).End(xlUp).Row
Set cr = TS.Range(Cells(1, 19), Cells(lr, lastcol))
If TS.AutoFilterMode = True Then TS.AutoFilterMode = False
With cr
.AutoFilter field:=1, Criteria1:="<>"
.SpecialCells(xlCellTypeVisible).Copy
End With
TS.AutoFilterMode = False
With TS
.Cells(lr + 1, 1).Select
.Paste
End With
cr.Delete xlShiftToLeft
lastcol = lastcol - 18
Next i
Application.CutCopyMode = False
Application.ScreenUpdating = True

Finish:
TS.UsedRange.Sort Key1:=Range("A1"), Order1:=xlAscending
Exit Sub
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,447
Messages
6,124,907
Members
449,194
Latest member
JayEggleton

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