Macro - If Cell in Column is Blank then input Text into Same row in another Column

tribalbydesign

New Member
Joined
Sep 15, 2008
Messages
20
Hello,

I am looking for a Macro that will search a column for blank cells, and when one is found will add text to the same row in another column. For example: The below is a spreadsheet. I am trying to find something that will search through column "C" in this case and add text (of my choice) to Column "A" if the cell is Blank. So Since cell C1 is Blank then Type "ERROR" is cell A1.
Code:
   A B C D E
1 X X    X X
2 X X    X X
3 X X X X X
4 X X X    X
5 X X X X
Thanks!
 
Last edited:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
try
Code:
Sub test()
Dim myChoice, myCol As String, replaceCol As String
Dim x As String, y As String
myCol = Application.InputBox("Which column to find blank?", Type:=2)
replaceCol = Application.InputBox("Which column to change?", Type:=2)
myChoice = Application.InputBox("What to replace?", Type:=3)
If Not IsNumeric(myChoice) Then myChoice = Chr(34) & myChoice & Chr(34)
x = Range(myCol & "1:" & myCol & Range(replaceCol & Rows.Count).End(xlUp).Row).Address
y = Range(replaceCol & "1:" & replaceCol & Range(replaceCol & Rows.Count).End(xlUp).Row).Address
Range(y).Value = Evaluate("if(" & x & "<>""""," & y & "," & myChoice & ")")
End Sub
 
Last edited:
Upvote 0
Jindon,

That is rather interesting I haven't used the text boxs yet.. However it actually didn't work on my Spreadsheet (I may be doing something wrong). When asked what column I answered S and to change answered C. Then typed in the text I wanted it to display and it didn't do anything. I am looking more for an automatic macro. The columns and terms are not going to change so I can modify the macro to work for what I need. I'm just not sure how to code it. I definetely am going to look into learning how to use the text box's though. I have a couple applications I could use that for! (That comes after learning the rest though..)

Thanks!
 
Upvote 0
It is working here.
If columns are fixed
Code:
Sub test()
Dim myChoice
Dim x As String, y As String
myChoice = "Error"
If Not IsNumeric(myChoice) Then myChoice = Chr(34) & myChoice & Chr(34)
x = Range("s1:s" & Range("c" & Rows.Count).End(xlUp).Row).Address
y = Range("c1:c" & Range("c" & Rows.Count).End(xlUp).Row).Address
Range(y).Value = Evaluate("if(" & x & "<>""""," & y & "," & myChoice & ")")
End Sub
 
Last edited:
Upvote 0
jindon,

Still not sure if I am doing this correctly. I am still getting nothing.. My columns are fixed. Only want it to look in S and if the cell is blank then populate "HBL NOT ISSUED" in column C.

I kinda modified a code I was already using to try and work for this application, however it only goes down so far and then stops. And it's not only placing the text in the cells in column C for the Cells that are blank in column S, but it is putting it in for every line, so I think I need to try again.. That and I just noticed It's changing the header on the column.

** SIDENOTE ** - I probably should have mentioned this before, but in column S there could be data in row 2 no data in row 3, and data in 4-6.. It's sporadic. However they are always in date format.

Code:
For i = Cells(columns.Count, "S").End(xlUp).Column To 1 Step -1
           If InStr(Cells(i, "S").Value= "" ) Then
              Cells(i, "C").Value = "HBL NOT ISSUED"
           End If
         Next i

I'm using XP and Excel 2003 if that make a difference. I believe now tht I look over I am using the wrong string (InStr) for my application. But I am doing this to learn, so my mistakes are not worthless... :rolleyes:.. as long as I learn something from them. Thanks!
 
Upvote 0
Maybe you have different length of each column
try this
Code:
Sub test()
Dim myChoice
Dim x As String, y As String, z As Long
myChoice = "Error"
If Not IsNumeric(myChoice) Then myChoice = Chr(34) & myChoice & Chr(34)
z = ActiveSheet.Cells.SpecialCells(11).Row
x = Range("s1:s" & z).Address
y = Range("c1:c" & z).Address
Range(y).Value = Evaluate("if(" & x & "<>""""," & y & "," & myChoice & ")")
End Sub
 
Upvote 0
jindon,

Well thats closer! Now it is populating every line in column C in my spreadsheet with a 0 until it hits the first blank line, and then is adding the text to column C.

Would the fact that the data in column S is a date (1/1/08) have any effect on this line of code?
Code:
If Not IsNumeric(myChoice) Then myChoice = Chr(34) & myChoice & Chr(34)

The spreadsheet is based on rows w/ row 1 being a header for the columns, so every column is the same length, and every row the same width (in number of cells).
Maybe this could help as well, if you don't mind, could you explain the bottom row of code? I'm not quite understanding what it is actually doing. Maybe i'm not modifying something I need to be before running?

Thanks!
 
Upvote 0
That's because I haven't got correct last row of the data.

Which column will have more rows ?
Or do you have fixed rows ?
 
Upvote 0
Code:
Application.Intersect(Range("C:C").SpecialCells(xlCellTypeBlanks).EntireRow, Range("A:A")).Value = "error"
or
Code:
Application.Intersect(Range("S:S").SpecialCells(xlCellTypeBlanks).EntireRow, Range("C:C")).Value = "HBL Not Issued"
 
Upvote 0
Hi all,
Sorry to dig out such an old topic but I have got very similar problem.

Dim myChoice
Dim x As String, y As String
myChoice = "A/R"
If Not IsNumeric(myChoice) Then myChoice = Chr(34) & myChoice & Chr(34)
x = Range("k1:k" & Range("g" & Rows.Count).End(xlUp).Row).Address
y = Range("g1:g" & Range("g" & Rows.Count).End(xlUp).Row).Address
Range(y).Value = Evaluate("if(" & x & "<>""""," & y & "," & myChoice & ")")

That is a small part of my macro, the problem is that at the moment when excel finds blank cell in column K it will replace whatever is in the same row in column G with A/R and I would like it to add A/R not replace the data. Sorry for my english, thanks in advance!
Lukasz
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,208
Members
448,554
Latest member
Gleisner2

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