ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   For Each ws autofill (https://www.excelbanter.com/excel-programming/405945-each-ws-autofill.html)

Karen53

For Each ws autofill
 
Hi,

I have tried various combinations to try and get this to work to no avail.
It is only to fun on those worksheets between the 'First' worksheet and
'Last" worksheet. What is wrong with my code? I'm getting a 'Select method
of range class failed'.

Sub AutofillFormula()

Dim ws As Worksheet
Dim FirstIndex As Long
Dim LastIndex As Long

FirstIndex = Firstpg.Index
LastIndex = Lastpg.Index

For Each ws In ThisWorkbook.Worksheets

If ws.Index FirstIndex Then
If ws.Index < LastIndex Then

ws.Range("N49").Select
Selection.AutoFill Destination:=Range("N49:N349"),
Type:=xlFillDefault
ws.Range("N49:N349").Select

End If
End If
Next ws

End Sub
--
Thanks for your help.
Karen53

Bernie Deitrick

For Each ws autofill
 
Karen,

You cannot select a range on a worksheet that is not active.

ws.Select
ws.Range("N49").Select
Selection.AutoFill Destination:=ws.Range("N49:N349"), _
Type:=xlFillDefault
ws.Range("N49:N349").Select

HTH,
Bernie
MS Excel MVP

"Karen53" wrote in message
...
Hi,

I have tried various combinations to try and get this to work to no avail.
It is only to fun on those worksheets between the 'First' worksheet and
'Last" worksheet. What is wrong with my code? I'm getting a 'Select
method
of range class failed'.

Sub AutofillFormula()

Dim ws As Worksheet
Dim FirstIndex As Long
Dim LastIndex As Long

FirstIndex = Firstpg.Index
LastIndex = Lastpg.Index

For Each ws In ThisWorkbook.Worksheets

If ws.Index FirstIndex Then
If ws.Index < LastIndex Then

ws.Range("N49").Select
Selection.AutoFill Destination:=Range("N49:N349"),
Type:=xlFillDefault
ws.Range("N49:N349").Select

End If
End If
Next ws

End Sub
--
Thanks for your help.
Karen53





All times are GMT +1. The time now is 06:56 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com