error 1004 autofilter method of range class failed

ExcelDennis

New Member
Joined
Aug 12, 2013
Messages
25
Hi All,

Using 3 lines of code below i run into error 1004 autofilter method of range class failed on the last line.

Range("C12:P12").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.AutoFill Destination:=Range("C12:P12"), Type:=xlFillWeekdays

Could someone please help me to make these 3 lines of code work.

Many thanks in advance

Regards, Dennis
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You will need to tell us what you are trying to do.
The error you have reported does not fit with those lines of code.
 
Upvote 0
You will need to tell us what you are trying to do.
The error you have reported does not fit with those lines of code.

I have a date in cell P12. I drag the date to cell C12. In the range "P12:C12 two columns are hidden. (column H and column I

I want the drag to skip the hidden columns -> Selection.SpecialCells(xlCellTypeVisible).Select

The range can only contain weekdays -> Selection.AutoFill Destination:=Range("C12:P12"), Type:=xlFillWeekdays

So the result is

Column C Column D Column E Column F Column G Column J Column K Column L Column M Column N Column O Column P
16-8-2019 | 19-8-2019 | 20-8-2019 | 21-8-2019 | 22-8-2019 | 23-8-2019 | 26-8-2019 | 27-8-2019 | 28-8-2019 | 29-8-2019 | 30-8-2019 | 02-09-2019

Hope this is more clear.
Regards and many thanks for your help
 
Upvote 0
In that case try
Code:
Sub ExcelDennis()
   Range("P12").AutoFill Range("j12:P12"), xlFillWeekdays
   Range("G12").Value = Application.WorkDay(Range("J12"), -1)
   Range("G12").AutoFill Range("C12:G12"), xlFillWeekdays
End Sub
 
Upvote 0
In that case try
Code:
Sub ExcelDennis()
   Range("P12").AutoFill Range("j12:P12"), xlFillWeekdays
   Range("G12").Value = Application.WorkDay(Range("J12"), -1)
   Range("G12").AutoFill Range("C12:G12"), xlFillWeekdays
End Sub

Thanks it works. But the columns that are hidden are not always the same in the range.

So do u have a way to fix this.

Thanks again. Regards, Dennis
 
Upvote 0
How about
Code:
Sub ExcelDennis()
   Dim Ar As Areas
   Dim i As Long
   
   Set Ar = Range("C12:P12").SpecialCells(xlVisible).Areas
   For i = Ar.Count To 1 Step -1
      With Ar(i).Offset(, Ar(i).Count - 1).Resize(, 1)
         If .Value <> "" Then
            .AutoFill Ar(i), xlFillWeekdays
         Else
            .Value = Application.WorkDay(Ar(i + 1).Resize(, 1), -1)
            If Ar(i).Count > 1 Then .AutoFill Ar(i), xlFillWeekdays
         End If
      End With
   Next i
End Sub
 
Upvote 0
How about
Code:
Sub ExcelDennis()
   Dim Ar As Areas
   Dim i As Long
   
   Set Ar = Range("C12:P12").SpecialCells(xlVisible).Areas
   For i = Ar.Count To 1 Step -1
      With Ar(i).Offset(, Ar(i).Count - 1).Resize(, 1)
         If .Value <> "" Then
            .AutoFill Ar(i), xlFillWeekdays
         Else
            .Value = Application.WorkDay(Ar(i + 1).Resize(, 1), -1)
            If Ar(i).Count > 1 Then .AutoFill Ar(i), xlFillWeekdays
         End If
      End With
   Next i
End Sub


Thanks this is working

But is it possible to go one step further. what if I want this part of code
Range("C12:P12")

be replaced by
Range("Q12").Select
Range(Selection, Selection.End(xlToLeft)).Select because the size of the range could be different everytime.

Thanks again.<strike>



</strike>
 
Upvote 0
Maybe it would be a good idea for you to explain EXACTLY what you are after.
I am not a mind reader ;)
 
Upvote 0
Maybe it would be a good idea for you to explain EXACTLY what you are after.
I am not a mind reader ;)

Hi,

Figured that last question out myself. Many thanks for your great help.

I am not sure how to close this topic, but it can be closed.

Best regards,
Dennis
 
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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