Select Cells With Empty Value - vba

I'm creating a VBA script to color up some specifics cells, so far its going well, but now I need it to color some cells that have an empty value. These cells aren't blanks, they have formulas, but sometimes that formula will return "" as value. I need to select these specific cells, how can I do that?
Once again, these cells aren't empty or blank, only their value is null.

Not sure if that's what you want:
IsEmpty(ActiveCell) Checks if the cell is completely empty. No Formula or data whatsoever.
ActiveCell.Value="" Checks if the result of the cell is blank. (Blank data can be returned by a formula in the cell)
So if you want to check if the cell value is blank but there is a formula would be Not IsEmpty(ActiveCell) And ActiveCell.Value = ""
EDIT
Following clarification in comment the code would be:
Sub SelectEmpty()
Dim MyRange As Range
Dim Cell As Range
For Each Cell In Range("B18:H18")
If Cell.Value = "" Then
If MyRange Is Nothing Then
Set MyRange = Cell
Else
Set MyRange = Application.Union(MyRange, Cell)
End If
End If
Next Cell
MyRange.Select
End Sub

You can change the color of those cells, empty or "" with Conditional Formatting.
Insert a new rule, choose the option
Use a formula to determine wich values to format
And then write the formula:
=TRIM(B18)=""

Related

VBA check for empty cells in column and print value

In column J there are empty rows and rows with a value such as checked.
I have tried to write VBA code that prints "unchecked" where there is an empty cell AND this works, but when it hits a cell with a value (checked) it stops. And it won't go down to the next cell probably because I have formulas in the cell that prints nothing if not fullfilled, but it still contains that formula. In my case I have empty cells until J7 and then it starts again at J15. But this can change from time to time regarding source data.
The reason I want to do it like this is because I have a formula in column J that already have printed some values and then some VBA code that checks for other values in a different column and prints to column J. Column J is the filter master column sort of. So this is the way I have to do it I guess.
My code right now is,
Sub DoIfNotEmpty()
Dim ra As Range, re As Range
With ThisWorkbook.Worksheets("Sheet1")
Set ra = .Range("J:j25")
For Each re In ra
If IsEmpty(re.Value) Then
re.Value = "unchecked"
End If
Next re
End With
End Sub
Can I print to empty cells if the cell contains a formula which in this case has an if statement that is not filled?
Except from #Maxime Porté's points out that it should be .Range("J1:j25"). I guess the cells only look empty, but they are not.
A cell that contains an empty string, "", is not empty anymore, but it looks like it. You can test it like this:
In a new worksheet write in A1: ="" (there is no space in between!)
Copy A1 and special paste values in A1. A1 now looks to be empty.
Run Debug.Print IsEmpty(Range("A1").Value) in VBA and you get a FALSE.
The cell A1 is not empty any more, because it contains an empty string.
What can you do?
Sub DoIfNotEmpty()
Dim ra As Range, re As Range
With ThisWorkbook.Worksheets("Sheet1")
Set ra = .Range("J1:J25")
For Each re In ra
If IsEmpty(re.Value) or re.Value = vbNullString Then
re.Value = "unchecked"
End If
Next re
End With
End Sub
This will mark pseudo empty cells as "unchecked" too. But be aware that it also kills formulas that result in an empty string, "".
You could exploit the Specialcells() method of Range object:
Sub DoIfNotEmpty()
ThisWorkbook.Worksheets("Sheet1").Range("J1:J25").SpecialCells(xlCellTypeBlanks).Value = "unchecked"
End Sub
Or, if you have formulas returning blanks, then AutoFilter() "blank" cells and write in them
Sub DoIfNotEmpty()
With ThisWorkbook.Worksheets("Sheeet1").Range("J1:J25") '<--| reference your range (first row must be a "header")
.AutoFilter Field:=1, Criteria1:="" '<--| filter its empty cells
If Application.WorksheetFunction.Subtotal(103, .cells) > 1 Then .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).Value = "unchecked" '<--| if any cell filtered other than headers then write "unchecked" in them
.Parent.AutoFilterMode = False
End With
End Sub

Excel vba Autofill only empty cells

I have a column A with data up to A300.
In this range, some of theses cells are empty, some contain values.
In VBA, I set the formula of the cell A1 then I use the autofill function to set it all over my column (up to A300) like this :
ws.Range("A1").Select
Selection.AutoFill Destination:=ws.Range(ws.Cells(1, 1), ws.Cells(300, 1))
My problem is that datas contain on some cells are erased too ! I'm trying to autofill like it but only throught the empties cells.
I tried to add a filter on my worksheet like this :
ws.Range("$A$1:$A$300").AutoFilter Field:=1, Criteria1:="="
Then I reused the autofill function, but it seems to fill thourght the filtered cells...
Can't we add a parameter like "only empties cells" to the autofill function ? Something like this :
Selection.AutoFill Destination:=ws.Range(ws.Cells(1, 1), ws.Cells(300, 1)), Criteria1:="="
Thanks for your replies !
with data like:
I would do a single copy rather than a fill-down:
Sub luxation()
Range("A1").Formula = "=ROW()"
Dim rDest As Range
Set rDest = Intersect(ActiveSheet.UsedRange, Range("A1:A300").Cells.SpecialCells(xlCellTypeBlanks))
Range("A1").Copy rDest
End Sub
with this result:
NOTE:
The formulas adjust after being copied.
EDIT#1:
Please note that there are some circumstances under which this code will not work. It is possible that UsedRange my not extend down to cell A300.
For example, if the worksheet is totally empty except for a formula in A1 and some value in A3. In this case Rdest will only include the single cell A2. The code will leave A4 through A300 untouched.
Assuming you want static values, I would use a loop. The one below will fill all empty cells with poop:
Sub AllFillerNoKiller()
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
For Each c In ws.Range("A1:A300")
If c.Value = "" Then c.Value = "poop"
Next
End Sub
Apologies, I miss-understood you question - Want to fill all blank cells with the value in A1? - here you go:
Sub Replace()
If Trim(Range("A1").Value) = "" Then
MsgBox "There's no value in A1 to copy so there's nothing to copy to all blank cells", vbInformation, "Nothing in A1"
Exit Sub
Else
Range("A1:A300").SpecialCells(xlCellTypeBlanks).Select
Selection.Value = Range("A1").Value
End If
End Sub
You can also use below code:
stAddress = Sheet1.Range("A1").CurrentRegion.SpecialCells(xlCellTypeBlanks).Address
Sheet1.Range(st).Value = "Empty"

Excel VBA iterate over all used cells and substitute their values

This is my first time ever working with VBA and Excel and my problem is probably caused by a big lack of knowledge on my part.
I have a one column list of city names (containing 800 items, hence I'm looking to automatically replace them) and some chunks of text in which the term "cityname" occurs multiple times and needs to be replaced by the correct city name, which in my setup would be the value of the first column cell in the same row.
So I found this: How to iterate through all the cells in Excel VBA or VSTO 2005
and found the InStr and Substitute functions by looking through the Excel VBA reference.
Using the iteration like this works fine:
Sub MySubstitute()
Dim cell As Range
For Each cell In ActiveSheet.UsedRange.Cells
If InStr(cell.Value, "cityname") > 0 Then
MsgBox "Hello World!"
End If
Next cell
End Sub
I get a message box for every "cityname" in my sheet (a test sheet with only 2 rows).
However when I add what I really want to achieve I get a Runtime Error (1004):
Sub MySubstitute()
Dim cell As Range
For Each cell In ActiveSheet.UsedRange.Cells
If InStr(cell.Value, "cityname") > 0 Then
cell.Value = WorksheetFunction.Substitute(cell.Value, "cityname", Cells(cell.Row, A))
End If
Next cell
End Sub
So I guess something in that line is wrong but I can't figure out what. Any hints to what my mistake is are appreciated, thanks :)
You can use the Range.Replace Method and replace all at once no need to iterate.
Sub MySubstitute()
Dim rng As Range
Set rng = ActiveSheet.UsedRange.Cells
rng.Replace "cityname", "correctcityname", xlPart
End Sub
You should change:
cell.Value = WorksheetFunction.Substitute(cell.Value, "cityname", Cells(cell.Row, A))
by
cell.Value = WorksheetFunction.Substitute(cell.Value, "cityname", Cells(cell.Row, 1))

How do I put VLOOKUP in a cell and paste it down?

I'm using VlookUp in VBA, but it only works for one cell.
For Each MyCell In Selection
MyCell = application.WorksheetFunction.VlookUp(Range("C2"), Range("A2:B50000"), 2 , 0)
Next
I want to set a VlookUp in a cell, then roll this to all other cells. But I am only able to put the formula in the first cell, then a double click to others.
Try this instead:
With Selection
.FormulaR1C1 = "=VLOOKUP(RC3,R2C1:R50000C2,2,FALSE)"
.Value = .Value '// Comment out if you don't want to paste values.
End With
This enters the VLOOKUP() formula into each cell using a relative reference for the lookup value's row and then effectively pastes values to replace the formula.
If you really want you can use VBA syntax:
For Each myCell In Selection
myCell.Value = WorksheetFunction.Vlookup(Cells(myCell.Row, 3), Range("A2:B50000"), 2, False)
Next

How to select and paste values in filtered cells in Excel using VBA

Please see below picture in which a filter is applied in column a to choose Item "b" and then updated "OK" in comment field. Do you have any idea how this can be done through macro? Also, I need to know how to select the visible cells in a column when a filter is on and filtered.
Sub Copy_Filtered_Cells()
Set from = Application.InputBox("Select range of Cell to copy", Type:=8).SpecialCells(xlCellTypeVisible)
'Selection.SpecialCells(xlCellTypeVisible).Select
'Set from = Selection
Set too = Application.InputBox("Select range to Paste Copied Cells", Type:=8)
For Each Cell In from
Cell.Copy
For Each thing In too
If thing.EntireRow.RowHeight > 0 Then
thing.PasteSpecial
Set too = thing.Offset(1).Resize(too.Rows.Count)
Exit For
End If
Next
Next
End Sub
it might work for you.