VBA Excel: Extend a formula to the last row

Solved
Alice -  
PhilDk Posted messages 20 Status Membre -
Hello,

I'm simply looking for a VBA code to automatically extend a formula from cell C6 to the last cell in the row without specifying it.
Currently, the following code works very well:

Range("C6").Select

Selection.Autofill Destination:=Range("C6:C450")

But since the last cell is not always the 450th, I would like it to be defined automatically and on a case-by-case basis.

Thank you!

Configuration: Windows XP / Firefox 10.0.11

4 réponses

pijaku Posted messages 13513 Registration date   Status Modérateur Last intervention   2 771
 
Sure.
So now we just need to replace in
Destination:=Range("C6:C450")
the 450 with the variable DernLigne.
Let's take the opportunity to remove those unnecessary .Select and Selection...
For example:
Dim DernLigne As Long DernLigne = Range("C" & Rows.Count).End(xlUp).Row Range("C6").AutoFill Destination:=Range("C6:C" & DernLigne)


However, previously, we have been looking for the last non-empty row in column C. Now, in column C, we are trying to fill in with a formula... Therefore, for now, column C contains nothing, if I'm not mistaken... So it doesn't make sense to look for the last non-empty row in column C.

--
Best regards,
Franck
14
Alice
 
To clarify everything, I entered the following code:

Sub Extension_formule()

Dim DernLigne As Long

DernLigne = Range("C" & Rows.Count).End(xlUp).Row

Range("C6").AutoFill Destination:=Range("C6:C" & DernLigne)

End Sub

An error message came out, stating: "the Autofill method of the Range class failed".

Regarding your subsequent remark, I thought that by responding 450 to your question about the number displayed in the dialog box when entering the suggested code at the beginning of the forum, namely:

Sub Test()
Dim DernLigne As Long
DernLigne = Range("C" & Rows.Count).End(xlUp).Row
MsgBox DernLigne
End Sub

you would easily deduce that the famous column C is in a table of 450 rows. That’s actually the whole point of extending a formula...
1
pijaku Posted messages 13513 Registration date   Status Modérateur Last intervention   2 771
 
Hello,

Actually, if you want to extend your formula from row 6 to row 450, it's likely that your column C is empty, to begin with.
Or you have data (or formulas) that you want to replace.

In the first case (empty column C), you need to calculate the last row based on another column. For example, column B may be filled.
In this case, the code should be as follows:
Sub Extension_formule() Dim DernLigne As Long DernLigne = Range("B" & Rows.Count).End(xlUp).Row Range("C6").AutoFill Destination:=Range("C6:C" & DernLigne) End Sub


In the second case, if your column C is already filled, the code given above is supposed to work.
Unless something is preventing it…
But here, it's a peculiar aspect of your workbook that we cannot know without seeing it…

I have tested and was able to reproduce the error "the Autofill method of the Range class failed" when column C only contains a formula in C6 and nothing else…

Try again with the last row calculation of another column and, if it still doesn't work, send us an example workbook via cjoint.com.
1