Run-time error 1004 autofilter method of range class failed

SGP123

New Member
Joined
Oct 3, 2017
Messages
10
Hi guys,

I have a problem with the following code in VBA. I'm very new to VBA, so I can't see what seems to be the problem.... The followin code gives me Run-time error 1004 autofilter method of range class failed....
Rich (BB code):
Sub SplitData()
Dim ark As Worksheet
Dim arkNew As Worksheet
Dim Data As Range
Dim rfll As Range
Dim Rum As String
Set ark = ThisWorkbook.Sheets("Alle Rum")


With ark
Set Data = .Range(.Cells(1, 1), .Cells(.Rows.Count, 9).End(xlUp))
.Columns(.Columns.Count).Clear
.Range(.Cells(3, 6), .Cells(.Rows.Count, 6).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, copyToRange:=.Cells(1, .Columns.Count), unique:=True


For Each rfll In .Range(.Cells(1, .Columns.Count), .Cells(.Rows.Count, .Columns.Count).End(xlUp))
Rum = rfll.Text


If WksExists(Rum) Then
Sheets(Rum).Cells.Clear
Else


Set arkNew = Sheets.Add
arkNew.Move After:=Worksheets(Worksheets.Count)
arkNew.Name = Rum
End If


Data.AutoFilter Field:=6, Criteria1:=Rum                      IT'S HERE THE ERROR IS!
Data.Copy Destination:=Worksheets(Rum).Cells(1, 1)
Next rfll
End With
ark.Columns(Columns.Count).ClearContents
Data.AutoFilter


End Sub


Function WksExists(wksName As String) As Boolean
On Error Resume Next
WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
End Function
I really appreciate the help you can give me!

THANKS!
 
Last edited by a moderator:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Welcome to the Board!

Try verifying that your variables are returning the correct values before that line, i.e. see what these two Message Boxes return (put them just before the line that is erroring out)
Code:
MsgBox Data.Address
MsgBox Rum
 
Upvote 0
Thank you!

My variables returning the right values:
MsgBox Data.Address returns $A$1:$I$72 (that's the size of my table)
Msgbox Rum returns 308 (that's one of numbers which I would like to split into new sheets)

Sorry I am so lost - I will be gratefull for your help!
 
Upvote 0
MsgBox Data.Address returns $A$1:$I$72 (that's the size of my table)
Do you have any merged or protected cells in your range?
 
Upvote 0
Actually my data come from a query from the program MS query. It has crossed my mind, that it could be the problem of splitting the data into multiple sheets.
I got my data from that program because I have to update the data everyday.
 
Upvote 0
Now it can split the data, but the multiple sheets just copy the wohole tabel, instead of splitting the data based on one of the columns... Do you have a solution for that???

Thanks.....
 
Upvote 0
Without seeing what your data actually looks like, I don't think there is much I can do.
 
Upvote 0

Forum statistics

Threads
1,215,831
Messages
6,127,145
Members
449,363
Latest member
Yap999

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