Adding formula to col A if col B has data

andysh

Board Regular
Joined
Nov 8, 2019
Messages
111
Hi,

I'm trying to add a formula into any cells in col A of a sheet where col B has data. I need the code to:

Look for last row in col B
If col B has data but A is empty
col A cell value = "Formula"

So in the below snip, the cells highlighted in yellow would be the ones where the formula is added.

They will be the last rows in each column, if it makes things easier. So basically the code would make both columns the same length by filling col A with the formula.

I did try this code but it only works if text is typed into cells, it doesn't work if multiple cells are pasted into col B.

VBA Code:
If target.Column <> 2 Or target.Cells.Count > 1 Then Exit Sub

If target.Offset(0, -1) = "" Then
target.Offset(0, -1) = “TheFormula”

End If

That was more difficult to explain than I thought, so I hope it makes sense.

1605553656828.png
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
How are you trying to run the code?
 
Upvote 0
Preferably from a command button with some other code that pastes the data into col B.

The snippet of code above was in a worksheet_change sub on the sheet where the data is pasted but that appears to be the wrong approach?
 
Upvote 0
Ok, how about
VBA Code:
Sub andysh()
   With Range(Range("A" & Rows.Count).End(xlUp).Offset(1), Range("B" & Rows.Count).End(xlUp).Offset(, -1))
      .FormulaR1C1 = "=rc[1]"
   End With
End Sub
 
Upvote 0
That does pretty much exactly what I want, thanks.

How could I get it to do that on another sheet e.g.

With Workbooks("Other workbook").Worksheets("Sheet1")
With Range(Range("A" & Rows.Count).End(xlUp).Offset(1), Range("B" & Rows.Count).End(xlUp).Offset(, -1))
.FormulaR1C1 = "=rc[1]"
End With
End With
 
Upvote 0
Like
VBA Code:
Sub andysh()
   With Workbooks("Other workbook").Worksheets("Sheet1")
      With .Range(.Range("A" & Rows.Count).End(xlUp).Offset(1), .Range("B" & Rows.Count).End(xlUp).Offset(, -1))
         .FormulaR1C1 = "=rc[1]"
      End With
   End With
End Sub
You just needed to add a full stop infront of each Range
 
Upvote 0
****, when I was trying every permutation of that I missed the stop in front of the range in the first bracket. I had 'With .Range(Range'

Lesson learned, cheers Fluff
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,284
Members
448,885
Latest member
LokiSonic

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