VBA - Enter value if column not blank

albytross

New Member
Joined
Sep 22, 2021
Messages
24
Office Version
  1. 365
Hi,

In sheet XYZ I have values in column B.

I need column A in XYZ to be populated with the value from cell A2 in worksheet "ABC" - but only as far as column B is populated.

Should be easy, maybe so easy nobody has asked before because I can't find a solution online.

Would appreciate some help if anyone has a moment.
 

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 assume you meant "but only as far as column B in sheet XYZ is populated".
Thus, in XYZ-A2:
Excel Formula:
=IF(B2<>"",ABC!A2,"")
 
Upvote 0
Hi Anthony, thanks - this is how i've written it in VBA but its coming up with Run-Time error 1004.

Would you know where I've gone wrong?

VBA Code:
Sub XYX_Populate()
    ActiveSheet.Name = "XYZ"
    Range("A:A").Value = "=IF(B2<>"",ABC!A2,"")"
    
End Sub
 
Upvote 0
Hi,
Watch out Column A has over 1 Million Rows . !!! 1'048'576 to be precise ...
Better define your actual Range ...
 
Upvote 0
You can test following
VBA Code:
Sub XYZ_Populate()
Dim i As Long
Dim lastr As Long
lastr = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
    For i = 2 To last
        Range("A" & i) = Evaluate("=IF(B" & i & "<>"""",ABC!A" & i & ","""")")
    Next i
End Sub
 
Upvote 0
Hi,
Watch out Column A has over 1 Million Rows . !!! 1'048'576 to be precise ...
Better define your actual Range ...
thanks. I confined the range to A200 but still get the error. i figure its down to the actual syntax, but don't know where abouts..
 
Upvote 0
You can test following
VBA Code:
Sub XYZ_Populate()
Dim i As Long
Dim lastr As Long
lastr = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
    For i = 2 To last
        Range("A" & i) = Evaluate("=IF(B" & i & "<>"""",ABC!A" & i & ","""")")
    Next i
End Sub

Did it compile for you? I'm getting a syntax error
 
Upvote 0
Sorry for typo ...:oops:
just replace last by lastr
VBA Code:
Sub XYZ_Populate()
Dim i As Long, lastr As Long
lastr = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
For i = 2 To lastr
     Range("A" & i) = Evaluate("=IF(B" & i & "<>"""",ABC!A" & i & ","""")")
Next i
End Sub
 
Upvote 0
Once you have tested the macro, feel free to share your comments
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,795
Members
449,468
Latest member
AGreen17

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