Autofilling another sheet by running a macro on a different sheet

fnpb93

New Member
Joined
Jan 13, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi all!
Thanks for the help in advance.
VBA Code:
Sub GenerateInterval()
    Dim lastRow As Variant
    lastRow = Range("I5").Value
    With Worksheets("Backend")
        .Range("B4").Formula = "=Backend!B3+1"
        .Range("B4").AutoFill Range("B4:B" & lastRow)
    End With
End Sub
Let's say I have 2 sheets, Sheet1 and Backend
So I want to run this macro from Sheet1, and Sheet1 Cell I5 contains a value, which is the number of rows I want to autofill in the backend sheet.
Hence, from my code, after getting the value of I5 and putting it into a variable lastRow, I then activate/moved to the Backend sheet, and want to fill B4 and the rows below subsequently until it fills up to Row (I5) where I5 is an integer where the formula is shown.

Screenshots as follows:

1642389768712.png
1642389780903.png


However, it doesn't seem to work and gave me an error of Autofill method of Range Class failed. Is it because I didn't actually run the autofill formulas in the Backend spreadsheet? How do I do so?

Thanks!

May I know what I did wrongly?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
VBA Code:
'Try
Sub GenerateInterval()
    
    Dim lastRow As Long
    
    lastRow = Worksheets("Sheet1").Range("I5").Value
    Worksheets("Backend").Range("B4:B" & lastRow).Formula = "=Backend!B3+1"

End Sub
'or using SEQUENCE function:
Worksheets("Backend").Range("B4").Formula2 = "=SEQUENCE(Sheet1!I5-ROW()+1)"
 
Upvote 0
Solution
VBA Code:
'Try
Sub GenerateInterval()
   
    Dim lastRow As Long
   
    lastRow = Worksheets("Sheet1").Range("I5").Value
    Worksheets("Backend").Range("B4:B" & lastRow).Formula = "=Backend!B3+1"

End Sub
'or using SEQUENCE function:
Worksheets("Backend").Range("B4").Formula2 = "=SEQUENCE(Sheet1!I5-ROW()+1)"
Hmm it does actually work. Why does my original code not work though? Any brief explanations for it? Thanks for the help once again
 
Upvote 0
Yes, sorry, I side-stepped the question by using my preferred approach rather than .Autofill.

Rich (BB code):
.Range("B4").AutoFill .Range("B4:B" & lastRow)

In case it's hard to spot, I've changed the second Range to .Range

Do you see why it wasn't working / will now work? it's a common trap - omitting to fully qualify the relevant objects.
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,539
Members
449,038
Latest member
Guest1337

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