VBA to enter a formula into a specific cell and fill down: in every worksheet but one.

dougmarkham

Active Member
Joined
Jul 19, 2016
Messages
252
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,

I have 5 worksheets, 4 of which---Sheet 1, 2, 3, and 4 contain tables.
The fifth worksheet is called "MACRO" and I'm using it for a user interface, so I need to avoid running the macro in this worksheet.

What I want to do is put a formula into cell AF16 of worksheets 1 to 4, and autofill down through the range ("AF16:AF214"). Each time I am given worksheets 1 to 4, they come with different names, hence I've tried to get my VBA code to run on all worksheets except ws.name ("MACRO").

I have this VBA code.

Code:
Sub AutoFillDownAF15()    
    Dim xWs As Worksheet
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For Each xWs In Application.ActiveWorkbook.Worksheets
        If xWs.Name <> "MACRO" Then
            xWs.Range("AF16").FormulaR1C1 = "=IF(RC[-31]="""","""",R15C32)"
            xWs.Range("AF16").AutoFill Destination:=Range("AF16:AF214")
        End If
    Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

It does the job in the ok in Sheet 1, then enters the correct formula into AF16 of Sheet 2, but then the following error stops the job: Run-time error '1004': AutoFill method of Range class failed.

Would anyone be willing to show me how to modify the code so it inserts the formula into AF16, fills down to AF214, and then cycle through to the next worksheet and repeats?

Kind regards,

Doug
 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
try xWs.Range("AF16:AF214").FormulaR1C1 = "=IF(RC[-31]="""","""",R15C32)"

and comment out the autofill line
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,986
Members
449,060
Latest member
mtsheetz

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