VBA - Do a command in cell until adjacent cell is empty

Tash Point O

New Member
Joined
Feb 12, 2018
Messages
47
Hello -

I am trying to figure out how to copy and paste cell (for example) A1's content into cell A2, A3 and on but only if cell B2, B3 etc has content in it, otherwise, stop the copy/pasting. I also need this to be a relative reference. Please help :eeek::) TY so much




n5hkxt.jpg
[/IMG]
2ent6yc.jpg
[/IMG]
 
If Col B is values, rather than formulae, another option is
Code:
Sub FillDownColA()
   Dim Rng As Range
   With Range("B1", Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlConstants)
      For Each Rng In .Areas
         Rng.Offset(, -1).FillDown
      Next Rng
   End With
      
End Sub
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I have annotated it to explaoin everything, I hope it helps you to learn how to use VBa effectively
Code:
Dim outarr As Variant
' find last row in column B
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
' load all of columm A in to a variant array called cola
cola = Range(Cells(1, 1), Cells(lastrow, 1))
' load all of columm B in to a variant array called colb
colb = Range(Cells(1, 2), Cells(lastrow, 2))
' load Arow1 with the first number to  write down column
Arow1 = cola(1, 1)


' loop through all the rows
For i = 1 To lastrow
 ' if column B is blank don't wirte anything inot column but pick up the next valu'
 ' for Arow1 from the row below
 
 If colb(i, 1) = "" Then
   Arow1 = cola(i + 1, 1)
 Else
 ' colunm B is not blnk so write the number back into column A
   cola(i, 1) = Arow1
 End If
Next i




' Now write the variant array back into the whole of column A
 Range(Cells(1, 1), Cells(lastrow, 1)) = cola
 
Upvote 0
I have annotated it to explaoin everything, I hope it helps you to learn how to use VBa effectively
Code:
Dim outarr As Variant
' find last row in column B
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
' load all of columm A in to a variant array called cola
cola = Range(Cells(1, 1), Cells(lastrow, 1))
' load all of columm B in to a variant array called colb
colb = Range(Cells(1, 2), Cells(lastrow, 2))
' load Arow1 with the first number to  write down column
Arow1 = cola(1, 1)


' loop through all the rows
For i = 1 To lastrow
 ' if column B is blank don't wirte anything inot column but pick up the next valu'
 ' for Arow1 from the row below
 
 If colb(i, 1) = "" Then
   Arow1 = cola(i + 1, 1)
 Else
 ' colunm B is not blnk so write the number back into column A
   cola(i, 1) = Arow1
 End If
Next i




' Now write the variant array back into the whole of column A
 Range(Cells(1, 1), Cells(lastrow, 1)) = cola

I read that w/as much enthusiasm as I imagine a kid reading the first page of the newest Harry Potter release at midnight (back in the day). Thank you!
 
Upvote 0
If Col B is values, rather than formulae, another option is
Code:
Sub FillDownColA()
   Dim Rng As Range
   With Range("B1", Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlConstants)
      For Each Rng In .Areas
         Rng.Offset(, -1).FillDown
      Next Rng
   End With
      
End Sub

I will test this out too, thank you!
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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