Loop through cells in range to find cells manually set to blank - excel

I am attempting to use the following code to loop through a range of cells, searching for cells that contain ="":
Sub ReplaceEmptyCells()
Dim i As String
Dim k As String
Dim cell As Range
i = "=" & Chr(34) & Chr(34)
k = "#N/A"
For Each cell In ActiveSheet.Range("C1:AA51") 'Desired range to search through
If cell.Value = i Then
cell.Replace What:=i, Replacement:=k, MatchCase:=True
End If
Next cell
End Sub
This value has been set manually on another sheet and the cells in question have been copied and pasted as a link (this was done so as to avoid the empty cells being converted to 0s after the paste, while maintaining 0s in the original data).
The intent is for the content of the "blank" cells on the new sheet to be "#N/A". Why doesn't the above Sub work?

Why loop when you can just use:
ActiveSheet.Range("C1:AA51").Replace What:=i, Replacement:=k, Lookat:=xlWhole

Related

Is there a way to reference if cells have decimal point in vba?

I want to have create some code in VBA that will look through the cells in a worksheet in column B. If the code finds a cell value with a decimal (opposed to a whole number e.g. not 1, but 1.1) then print the cell value to right off it in another sheet.
I know it will be a loop and it will use offset hit etc. I havent tried this code in VBA i have just typed it as an example of what i will do in the question.
For each cell in Sheets("Sub Tasks").Range("B1:B" & LastRow)
If cell = '(DECIMAL FORUMLA) Then
You may use something like this:
Dim cell As Range
For Each cell In Sheets("Sub Tasks").Range("B1:B" & LastRow)
If IsNumeric(cell.Value) Then
If Val(cell.Value) <> Int(cell.Value) Then
' The number contains decimals.
Dim valueToTheRight As Variant
valueToTheRight = cell.Offset(0, 1).Value
' TODO: Add `valueToTheRight` into the appropriate place of the other sheet.
End If
Else
' The cell value is not a number.
' TODO: either do something about it or remove the `else` branch to ignore it.
End If
Next
You could evaluate the cells content?
Sub Demo()
Dim cell
For each cell in Sheets("Sub Tasks").Range("B1:B" & LastRow)
If IsNumeric(c.Value2) And InStr(c.Value2, Application.DecimalSeparator) Then
Debug.Print c.Value2
End If
Next c
End Sub
IsNumeric will test if the cell holds a number. i.e. skipping any strings with full stops in. and Instr(c.value2, Application.DecimalSeparator) will test for the decimal

Within a range of cells, how do I enter a formula based on condition with vba?

I'm new to VBA. I have a range of cells in column B with the format:
1###-2#-3##-4#
I want to enter a mid formula to get the last cell based on the fact that:
the next cell is empty
the cell matches the regex pattern
My code doesn't give me any errors but doesn't run.
Option Explicit
Sub DeptProgram()
Dim regex As String
Dim Pattern As String
Dim cell As Range
regex = "[0-9]{4}-[0-9]{2}-[0-9]{3}-[0-9]{2}"
For Each cell In Range("B1:B600")
If Pattern = regex And ActiveCell.Offset(1, 0) <> "" Then
ActiveCell.Offset(0, 2).Formula = "MID(B" & "activecell.Row.Offset(0,-2), 6, 11)"
End If
Next cell
End Sub
On another note: I used regex in VBA based on this article.
As per all the comments:
Option Explicit
Sub DeptProgram()
Dim cell As Range
For Each cell In Range("B1:B600")
if cell like "####-##-###-##" and cell.offset(1,0) = "" then
cell.Offset(2,0).Formula = "=MID(" & cell.Address & ", 6, 11)"
End If
Next cell
End Sub
After running:

How to use cell contents as inputs for range formulas in Excel VBA?

Excel VBA range formula is something like Sheet2.Range("A1").value.
You can, for example, refer to this address and change the value in that cell.
Now what I am looking for is to build this formula by using the contents of two cells; one cell says "Sheet2" and the other says "A1". How can I refer to these two cells in a formula so that it works the same way?
I have tried various dims and now end up without an error but not with the desired result. The current formula simply returns "Sheet2.Range("A1").value instead of its contents.
Sub AddressInCells()
'This is just for one reference. In reailty there is a table of references to loop through.
'For this example cell A1 in Sheet2 = "Text to take to sheet1"
Dim SheetSel As String
Dim CellSel As String
Dim ReferSel As String
With Sheet1
SheetSel = Cells(1, 1).Value
CellSel = Cells(1, 2).Value
ReferSel = SheetSel & ".range(" & Chr(34) & CellSel & Chr(34) & ").value"
MsgBox ReferSel
Sheet1.Range("D1").Value = ReferSel 'Results in "Sheet2.range("A1").value
Sheet1.Range("D2").Value = Sheet2.Range("A1").Value 'Results in Text to take to sheet1'
End With
End Sub
The expected result is that the formula works the "usual" way. It now simply returns a string.
I believe, instead you want to do:
ReferSel = Sheets(SheetSel).range(CellSel).value
You could try:
Option Explicit
Sub test()
Dim ws As Worksheet
Dim rng As Range
'With statement where the data are store. Change name if needed
With ThisWorkbook.Worksheets("Sheet1")
Set ws = ThisWorkbook.Worksheets(.Range("A1").Value)
Set rng = ws.Range(.Range("A2").Value)
End With
Debug.Print rng.Address
Debug.Print rng.Worksheet.Name
End Sub
You're trying to build a formula string, which follows this convention:
'<sheet name>!'<address>
The single-quotes are only strictly required if the sheet name contains a space, so this would be a valid formula:
=Sheet1!$A$1
But it's just good habit to always include them, so, you need to build your string from the values in those cells:
' offset the sheet name in single quotes, separate the sheet & address with !
ReferSel = "='" & SheetSel "'!" & CellSel
Sheet1.Range("D1").Formula = ReferSel

Deleting condition formatting for texts & blanks which may occur at any cell in range(b1:b54)

The conditional formattiong is applied to the range(B1:B54) which contains numbers, text & blank. Once this is done, I am required to re-colour cells in a column back to default one which are coloured either green or red from conditional formatting.
Can anybody give me small script to either delete the CF for texts & blanks in range(B1:B54).
You could try:
Option Explicit
Sub Delete_CF()
Dim rng As Range, cell As Range
With ThisWorkbook.Worksheets("Sheet1") 'Change if needed
'Set the range to loop
Set rng = .Range("B1:B54")
'Loop the range
For Each cell In rng
With cell
'Check if cell is empty or not numeric
If .Value = "" Or Not IsNumeric(.Value) Then
.FormatConditions.Delete
End If
End With
Next cell
End With
End Sub

VBA function to insert formula into cell with another vba function embedded

I have a cell that I would like to place an If function. The if function will display a 1 if the average of a selected range of cells is >=2, else 0. I would like to make a vba that will ask you to select the group of range and then place the formula onto the cell, not the value. That way if the average of the selected group of cells gets changed, the updated cell gets changed as well. I've tried doing this but it ends up with a #value error. Any help is appreciated
Sub update()
Dim siteRange As Range
ActiveCell.Select
If MsgBox("Would you like to update?", vbYesNo, "Update") = vbYes Then
Set siteRange = Application.InputBox("Select a range with the " & _
"appropraite sites.", "Sites", Type:=8)
ActiveCell.Value = "=If(Average(siteRange)>=2,1,0)"
Else
End If
End Sub
siteRange is a range object but you are treating it as a defined name. Concatenate the range object's address into the formula.
ActiveCell.Formula = "=If(Average(" & siteRange.address & ")>=2, 1, 0)"

Resources