return the next cell with text in a range that includes blank cells - excel-2007

I have a formula that should return the next text filled cell in a range of cells from a different worksheet, but "#NUM!" is returned instead of the text.
=if(ROW()-ROW(NoBlanksRange)+1>ROWS(BlanksRange)-COUNTBLANK(BlanksRange),"",INDIRECT(ADDRESS(SMALL((IF(BlanksRange<>"",ROW(BlanksRange),ROW()+ROWS(BlanksRange))),ROW()-ROW(NoBlanksRange)+1),COLUMN(BlanksRange),1)))

Try this regular formula:
=IF(ROWS(A$1:A1)>ROWS(NoBlanksRange),"",INDEX(BlanksRange,SMALL(INDEX((BlanksRange<>"")*ROW(BlanksRange)-ROW(INDEX(BlanksRange,1))+1,),COUNTBLANK(BlanksRange)+ROWS(A$1:A1))))

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

Hide the whole row when a range of cells is empty

I have a range of cells B2:AB40.
If every cell in each row within the the range is blank (by which I mean no text or numbers, just colour fill and border formatting), I want to hide the whole of the row using a macro.
e.g.
If every cell in the range B2:AB2 is blank then hide all of row 2.
If every cell in the range B3:AB3 is blank then hide all of row 3
If every cell in the range B4:AB4 is blank then hide all of row 4..etc etc etc
Up to and including row 40.
N.B. Each cell in column A and AC in every row adjacent to the specified range will always have text (someone's name and a formula result respectively) and this cannot be changed.
I have seen various ways of doing this based on a single cell but cannot seem to adapt them for my purposes.
Any help is appreciated.
Consider:
Sub RowHider()
Dim I As Long, wf As WorksheetFunction
Set wf = Application.WorksheetFunction
For I = 2 To 40
If wf.CountA(Range("B" & I & ":AB" & I)) = 0 Then
Rows(I).Hidden = True
Else
Rows(I).Hidden = False
End If
Next I
End Sub
Note the usage of a worksheet function in VBA.
Try this
Sub HideRangeIfEmpty()
If Application.WorksheetFunction.CountA(Range("b2:AB2")) = 0 Then
Range("b2:AB2").EntireRow.Hidden = True
End If
End Sub

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

Select Cells With Empty Value

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)=""

Copy cell value to all cells below it

I don't know how to write a macro that designates a cell within a column as a "master cell" (editable) copy that cells value to all the cells below it in that column, until it reaches a blank/clear formatted cell in column A. So I want it to look at column A to know when to stop copying the cell values in whichever column.
That is, Cell "C5" will be a master cell, the macro will copy it's value from "C6:C" but looking at column A's cell values to see if it has nothing in it and there's no formatting such as color fill, etc. and instead of the macro continuing on in column C to infinity (maximum increment for Excel) it will stop at A column's first blank cell row.
Sub Example()
Dim MasterValue As String
Dim StopRow As Long
Dim i As Long
'Get the master value
MasterValue = Range("C5").Value
'Get the first blank cell in column A
StopRow = Range("A1").End(xlDown).Row
'Start at row 6 and continue to the "Stop Row"
For i = 6 To StopRow
'Set every cell from row 6 in column 3 to the "Master Value"
Cells(i, 3).Value = MasterValue
Next
End Sub