VBA autofill until a value change on an adjacent column

Danilo Montoni

New Member
Joined
Aug 12, 2014
Messages
4
Im new to VBA so i would very much appreciate any help. I want to autofill a formula until a value change on an adjacent column. example:

<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">___A_____B_________

1 CAR =formula
2 CAR
3 CAR
4 GLO
5 GLO
6 GLO
</code>
In the example above: Autofill the formula from b1 to b3 (when the value on column A changes from CAR to GLO). PS: The number of rows with the imput CAR and GLO varies from month to month. Thanks!!!
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try this:

Sub vzorec()
radek = 1 'number of row where could start formulas
Do Until Cells(radek, 1).Value <> "CAR"
Cells(radek, 2).Formula = "=LEN(RC[-1])" 'the formula is up to you
radek = radek + 1
Loop


End Sub
 
Upvote 0
Try this:

Sub vzorec()
radek = 1 'number of row where could start formulas
Do Until Cells(radek, 1).Value <> "CAR"
Cells(radek, 2).Formula = "=LEN(RC[-1])" 'the formula is up to you
radek = radek + 1
Loop
End Sub

Hello Dura 1. thanks for your reply, but It didn't work for me.

Just to make more clear, i'm currently using the code below. But, as i said before, the number of rows with the imput CAR and GLO varies from month to month. So every month i have to make the adjustments manually.

Range("B1").Select
ActiveCell.FormulaR1C1= _
"=ROUND((SUMIFS(Ponto_Car!C[25],Ponto_Car!C[-8],R1C2,Ponto_Car!C[-6],RC[-7])+SUMIFS(Ponto_Car!C[26],Ponto_Car!C[-8],R1C2,Ponto_Car!C[-6],RC[-7])

Selection.AutoFill Destination:=Range("B1:B3"), Type:=xlFillDefault

Many Thanks!
 
Upvote 0
Hi, i think If there are first cells with "CAR" in the column A and below them cells with "GLO" the code is OK. You have to check if the name "CAR" in cells and the "CAR" in code is exactly the same. Or i dont understand the problem.:)
 
Upvote 0
Hi, i think If there are first cells with "CAR" in the column A and below them cells with "GLO" the code is OK. You have to check if the name "CAR" in cells and the "CAR" in code is exactly the same. Or i dont understand the problem.:)

Hi. Many thanks for your reply. Actually, on my original sheet, the names CAR and GLO are on column D. And the fórmula should start on column E, row 4 (cell E4). How does your code recognize that the "names" are on column D and the fórmula must be placed on cell E4 and then "dragged"/filled from E5 to E6 (for example)?

___D_____E_________

4 CAR =formula
5 CAR
6 CAR
7 GLO
8 GLO
9 GLO

I changed the radek = 4 (becouse formula starts on 4th row (cell E4)) Then i and ran the code but nothing happens.

Sub vzorec()

radek = 4 'number of row where could start formulas
Do Until Cells(radek, 1).Value <> "CAR"
Cells(radek, 2).Formula = "=LEN(RC[-1])" 'the formula is up to you
radek = radek + 1
Loop
 
Upvote 0
hi,if the column change, the code could by like this:
Sub vzorec()
radek = 4 'number of row where could start formulas
sloupec = 4 'number of column where are the names CAR and GLO
Do Until Cells(radek, sloupec).Value <> "CAR"
Cells(radek, sloupec + 1).Formula = "=LEN(RC[-1])" 'the formula is up to you
radek = radek + 1
Loop
End Sub
Hi. Many thanks for your reply. Actually, on my original sheet, the names CAR and GLO are on column D. And the fórmula should start on column E, row 4 (cell E4). How does your code recognize that the "names" are on column D and the fórmula must be placed on cell E4 and then "dragged"/filled from E5 to E6 (for example)?

___D_____E_________

4 CAR =formula
5 CAR
6 CAR
7 GLO
8 GLO
9 GLO

I changed the radek = 4 (becouse formula starts on 4th row (cell E4)) Then i and ran the code but nothing happens.

Sub vzorec()

radek = 4 'number of row where could start formulas
Do Until Cells(radek, 1).Value <> "CAR"
Cells(radek, 2).Formula = "=LEN(RC[-1])" 'the formula is up to you
radek = radek + 1
Loop
 
Upvote 0

Forum statistics

Threads
1,215,839
Messages
6,127,191
Members
449,368
Latest member
JayHo

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