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
 
Glad you sorted it & thanks for the feedback

Dear Sir,

Today with testing the code I run into an error

Sub Orgineel()
Dim Ar As Areas
Dim i As Long

Set Ar = Range("C12:AI12").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

This is the code, it works great only not when the first cel left from
AI12 is hidden, or the first two cells left from AI12 are hidden

Can u please help me to fix this error.

thanks again.

Regards, Dennis
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Ok, how about
Code:
Sub ExcelDennis()
   Dim AR As Areas
   Dim i As Long
   
   Set AR = Range("C12:AI12").SpecialCells(xlVisible).Areas
   For i = AR.Count To 1 Step -1
      With AR(i).Offset(, AR(i).Count - 1).Resize(, 1)
         If AR(i).Count > 1 And .Value <> "" Then
            .AutoFill AR(i), xlFillWeekdays
         ElseIf .Value = "" Then
            .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
Ok, how about
Code:
Sub ExcelDennis()
   Dim AR As Areas
   Dim i As Long
   
   Set AR = Range("C12:AI12").SpecialCells(xlVisible).Areas
   For i = AR.Count To 1 Step -1
      With AR(i).Offset(, AR(i).Count - 1).Resize(, 1)
         If AR(i).Count > 1 And .Value <> "" Then
            .AutoFill AR(i), xlFillWeekdays
         ElseIf .Value = "" Then
            .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

Works perfect.

Could u tell me also what the code would be for the other way around.

for example for a range ("AI12:BA12")

Thanks again

Regards, Dennis
 
Upvote 0
Maybe (untested)
Code:
Sub ExcelDennis()
   Dim AR As Areas
   Dim i As Long
   
   Set AR = Range("AI12:BA12").SpecialCells(xlVisible).Areas
   For i = 1 To AR.Count
      With AR(i).Resize(, 1)
         If AR(i).Count > 1 And .Value <> "" Then
            .AutoFill AR(i), xlFillWeekdays
         ElseIf .Value = "" Then
            .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
Maybe (untested)
Code:
Sub ExcelDennis()
   Dim AR As Areas
   Dim i As Long
   
   Set AR = Range("AI12:BA12").SpecialCells(xlVisible).Areas
   For i = 1 To AR.Count
      With AR(i).Resize(, 1)
         If AR(i).Count > 1 And .Value <> "" Then
            .AutoFill AR(i), xlFillWeekdays
         ElseIf .Value = "" Then
            .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,

Almost what I am looking for

Result of your Macro is

3-9-2019 | 4-9-2019 | 5-9-2019 | 6-9-2019 | 9-9-2019 | 4-9-2019 | 5-9-2019 | 6-9-2019 | 9-9-2019 | 10-9-2019 |
5-9-2019 | 6-9-2019 | 9-9-2019 |

Should be

3-9-2019 | 4-9-2019 | 5-9-2019 | 6-9-2019 | 9-9-2019 | 10-9-2019 | 11-9-2019 | 12-9-2019 | 13-9-2019 | 16-9-2019 | 17-9-2019 | 18-9-2019 | 19-9-2019 |
Will u please take another look. Thanks
 
Upvote 0
How about
Code:
Sub ExcelDennis()
   Dim AR As Areas
   Dim i As Long
   
   Set AR = Range("AI12:BA12").SpecialCells(xlVisible).Areas
   For i = 1 To AR.Count
      With AR(i).Resize(, 1)
         If AR(i).Count > 1 And .Value <> "" Then
            .AutoFill AR(i), xlFillWeekdays
         ElseIf .Value = "" Then
            .Value = Application.WorkDay(AR(i - 1).Offset(, AR(i - 1).Count - 1).Resize(, 1), 1)
            If AR(i).Count > 1 Then .AutoFill AR(i), xlFillWeekdays
         End If
      End With
   Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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