VBA Fill Empty Blanks In Column B if Value is Present in Column C - Help is Appreciated

kescco

Board Regular
Joined
Sep 14, 2009
Messages
174
I need for each Row in COLUMN B which is blank to be filled with "999"
if there is a positive value in COLUMN C (which can be negative).

So, in the example below, the blank spaces in Column B need to be filled with 999.

Please note this is part of a larger macro and all help is appreciated.

Column AColumn BColumn C
Row 131144
Row 234445
Row 3-444
Row 499
Row 5311-44

<tbody>
</tbody>
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Code:
Sub PopulateB()
    Dim lr As Long
    Dim i As Long
    lr = Range("C" & Rows.Count).End(xlUp).Row
    
    For i = 1 To lr
        If Range("B" & i).Value = "" And Range("C" & i).Value >= 0 Then
            Range("B" & i).Value = 999
        End If
    Next i
End Sub
 
Upvote 0
If Range("B" & i).Value = "" And Range("C" & i).Value >= 0 Then

Problem is that many of my entries in C will be Negative and thus the greater than or equal to will not work.


Could I use something like:

If Range("B" & i).Value = "" And Range("C" & i).Value <> "" Then

Kescco
 
Upvote 0
If Range("B" & i).Value = "" And Range("C" & i).Value >= 0 Then

Problem is that many of my entries in C will be Negative and thus the greater than or equal to will not work.


Could I use something like:

If Range("B" & i).Value = "" And Range("C" & i).Value <> "" Then

Kescco
Your OP is a bit ambiguous. Do you want to fill blank cells in col B if the col C companion cell is not blank, or only if there is a positive number in the C cell?
 
Upvote 0
Yes, you are correct that my original post was a bit confusing. First I say positive value and then say it can be negative. It has been a long day.

I only wanted to fill the blank values in B if there was anything present in C.

I went ahead and tested the code I replied with and it does work.

I appreciate all the help everyone.

Thank you.
 
Upvote 0
I only wanted to fill the blank values in B if there was anything present in C.
Here is a non-looping macro that should work for you...
Code:
Sub FillBlanksInColumnBifColumnCisFilled()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "C").End(xlUp).Row
  Range("B1:B" & LastRow) = Evaluate(Replace("IF((C1:C#<>0)*(B1:B#="""")" & _
                            ",999,IF(B1:B#="""","""",B1:B#))", "#", LastRow))
End Sub
 
Upvote 0
Here is a non-looping macro that should work for you...
Code:
Sub FillBlanksInColumnBifColumnCisFilled()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "C").End(xlUp).Row
  Range("B1:B" & LastRow) = Evaluate(Replace("IF((C1:C#<>0)*(B1:B#="""")" & _
                            ",999,IF(B1:B#="""","""",B1:B#))", "#", LastRow))
End Sub
Hello Mr. Rick, could you please explain the above code. I have similar query where I want to fill the values in blank cell from another cell same row.
 
Upvote 0

Forum statistics

Threads
1,203,125
Messages
6,053,648
Members
444,676
Latest member
locapoca

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