VBA marking cells Based on keywords - excel

I have defined a range which is filled with color based on the cell text. In the first example it searches the word "Auto" and marks the cell red. Where I get stuck is to extent the "if" command such as that the search criteria carries onto the column E (however stays at the same row as the word " Auto" states) looks after the word "Mortgage" and marks it red. Then it moves further the column up and marks the values red that are under 4yr, 6yr,7yr. The next step would be the same with the word "preferred". For simplicity I have included a picture. It seems to be a bit tricky and a hint would be appreciated.
Sub Schaltfläche2_Klicken()
Dim cell As Range
For Each cell In ws.Range("A1:A100")
If cell.Value = "Auto" Then
cell.Interior.Color = XlRgbColor.rgbRed
ElseIf cell.Value = "Mutti" Then
cell.Interior.Color = XlRgbColor.rgbRed
End If
Next
End Sub

How about the following:
Sub Schaltfläche2_Klicken()
Dim cell As Range
For Each cell In ws.Range("A1:A100")
If cell.Value = "Auto" Then
Range("A" & cell.Row, "E" & cell.Row, "G" & cell.Row, "I" & cell.Row, "K" & cell.Row).Interior.Color = vbRed
ElseIf cell.Value = "Mutti" Then
Range("A" & cell.Row, "E" & cell.Row, "G" & cell.Row, "I" & cell.Row, "K" & cell.Row).Interior.Color = vbRed
End If
Next
End Sub

Related

Allow alpha numeric values instead of isnumeric

I have the below code for entering a function and copying the orientation and borders of the above line.
But in this it only accept numeric values, how can i modify the code so that i can enter alpa numeric values in that cell.
Below is the code
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:B")) Is Nothing Then
If IsNumeric(Target.Value) Then ' Check if cell contains a numeric value
If Target.Value <> "" Then
Range("A" & Target.Row).Formula = "=IF(B" & Target.Row & "<>"""",ROW()-ROW($A$15)+1,"""")"
' Copy border, border color and orientation from row above
With Range("A" & Target.Row & ":H" & Target.Row)
.Borders.LineStyle = .Offset(-1, 0).Borders.LineStyle
.Borders.Color = .Offset(-1, 0).Borders.Color
.Orientation = .Offset(-1, 0).Orientation
End With
Else
' Check if entire row in column B is empty
If WorksheetFunction.CountA(Range("B" & Target.Row & ":H" & Target.Row)) = 0 Then
' Delete entire row
Rows(Target.Row).Delete
Else
' Clear contents of column A to H for the row where value was deleted in column B
Range("A" & Target.Row & ":H" & Target.Row).ClearContents
End If
End If
End If
End If
End Sub
Here's a small Function you could add to your code, to give it IsAlphaNumeric functionality.
Function IsAlphaNumeric(t) as Boolean
Dim i as Long
IsAlphaNumeric = True
For i = 1 To Len(t)
If Not (Mid(t, i, 1) Like "[A-z0-9]") Then
IsAlphaNumeric = False
Exit For
End If
Next
End Function
You can use it like this:
If IsAlphaNumeric(Target.Value) Then ' Check if cell contains alpha-numeric value

If cell.value A = x, then cell.value N = formula

Trying to fix my spreadsheet to paste a formula in column N, based on the value of column A.
Essentially, if cell A6 = "Text 1" then cell N6 = "Formula 1", if cell A7 = "Text 2" then cell N7 = "Formula 2" then have this loop through all rows.
Very amateur at VBA, so excessive googling has given me the below.
Sub Test()
lr = Cells.Find("*", Cells(1, 1), xlFormulas, xlPart, xlByRows, xlPrevious, False).Row
For Each Cell In Range("A6:A" & lr)
If Cell.Value = "Call Options" Then
Cell.Offset(0, 13).Value = "=-(B6*100)*E6"
ElseIf Cell.Value = "Put Options" Then
Cell.Offset(0, 13).Value = "=(B6*100)*E6"
End If
Next Cell
End Sub
At this point, I hit run and nothing changes. No error message or bug shows up.
Any help would be much appreciated.
If you want it to refer the cell in the same row you need to do this:
Cell.Offset(0, 13).Value = "=-(B" & Cell.Row & "*100)*E" & Cell.Row
If you want to make your If case insensitive use:
LCase(Cell.Value) = LCase("Call Options")
I would rewrite the code to the following:
Sub Test()
lr = Cells(1,Count.Rows).End(xlUp).Row
For i = 6 to lr
If Cells(i,1).Value = "Call Options" Then
Cells(i,14).Value = "=-(B" & i & "*100)*E" & i
ElseIf Cells(i,1).Value = "Put Options" Then
Cells(i,14).Value = "=(B" & i & "*100)*E" & i
End If
Next i
End Sub
The reason I would change this is you are just trying to find the last row, so let's not overcomplicate things. Also, when using numeric column values rather than alphabetical values, it makes it easier reference cells without relying on Offset.

Combine text of multiple cells

How to combine text of multiple cells for instance translate this excel formula to VBA code
Cell F3 should =Q3&"_"&A3&"_"&D3&"_"&(ROUND(M3/1000,1))&"k"
But I would Like it to repeat in every instant so in F99= Q99&"_"&A99&"_"&D99&"_"&(ROUND(M99/1000,1))&"k"
Using VBA Code:
For Each Cel In Range("A1:A100")
If Cel.Value <> "" Then Cel.Offset(0, 5).Value = *Excel Formula NEED* "Q3&"_"&A3&"_"&D3&"_"&(ROUND(M3/1000,1))&"k""*
Saying that if value in column A insert text in same row Column E
I also think that a formula (adapted a little to return "" in case of empty cells) would be the best. But, even if you did not answer my question and you want a solution in VBA, please, test the next code:
Sub testConcatenate()
Dim sh As Worksheet, cel As Range, lastRow As Long
Set sh = ActiveSheet 'please use your sheet here
lastRow = sh.Range("A" & Rows.count).End(xlUp).Row
For Each cel In Range("A1:A" & lastRow)
If cel.value <> "" Then
cel.Offset(0, 5).value = Range("Q" & cel.Row).value & "_" & cel.value & "_" & _
Range("D" & cel.Row).value & "_" & Round(Range("M" & cel.Row).value / 1000, 1) & "k"
End If
Next
End Sub
You maybe clarify here the meaning of "" in your formula, what "k" means and what the last character "*" is used for. I did not use it...

Returning FALSE when using multiple THEN statements

I have a macro that has conditional statements in. It works perfectly if I take out the second statement of coloring the cell with the error message. Instead when I add the color to the cell I am returned with a FALSE statement in my column.
It works perfectly here:
Sub trantype()
Dim cell As Range
Dim lastRow As Long
Sheets("1099-Misc_Form_Template").Select
lastRow = Range("B" & Rows.Count).End(xlUp).row
For Each cell In Range("C2:" & "C" & lastRow)
If cell.Value <> "C" And cell.Value <> "" Then cell.Offset(0, -2).Value = cell.Offset(0, -2).Value & ", Tran type error"
Next
End Sub
But when I add the second condition I am returned with a FALSE statement:
Dim cell As Range
Dim lastRow As Long
Sheets("1099-Misc_Form_Template").Select
lastRow = Range("B" & Rows.Count).End(xlUp).row
For Each cell In Range("C2:" & "C" & lastRow)
If cell.Value <> "C" And cell.Value <> "" Then cell.Offset(0, -2).Value = cell.Offset(0, -2).Value & ", Tran type error" & cell.Interior.ColorIndex = 37
Next
End Sub
I would like both conditions to be met, so the error message is printed in the offset cell and the cell with the error in to have color.
Put each command into its own row. The & ampersand is a text concatenation operator. It cannot be used to chain commands.
For Each cell In Range("C2:" & "C" & lastRow)
If cell.Value <> "C" And cell.Value <> "" Then
cell.Offset(0, -2).Value = cell.Offset(0, -2).Value & ", Tran type error"
cell.Interior.ColorIndex = 37
End If
Next
This way you need to use the End If statement, so don't forget that.

Trying to VBA Script some embedded if commands

What I am trying to do is get my macro to search the data in Column "E". If the cell value contains "string", then I would like to offset by one column to the left, verify if, in the new selected cell, cell value contains "". If the new selected cell value is "" then background color is 19, if it contains "*" then background color is -4142.
Here is the code I have so far:
Sub Set_Background_Color ()
lRow = Range("E" & Rows.Count).End(xlUp).Row
Set MR = Range("E2:E" & lRow)
For Each cell In MR
If cell.Value = "X" Then cell.Offset(, -1).Interior.ColorIndex = 19
Next
End Sub
I can't seem to figure out how to embed a new If statement after the Offset and before the .Interior.ColorIndex
I have tried this mess but you will see immediately that it does not work.
If cell.Value = "X" Then
ElseIf cell.Offset(, -1).Value = "" Then cell.Interior.ColorIndex = 19
Else: cell.Interior.ColorIndex = -4142
Any help is greatly apreciated!
So close!
Sub Set_Background_Color ()
Dim lRow As Long
Dim MR As Range
Dim cel As Range
lRow = Range("E" & Rows.Count).End(xlUp).Row
Set MR = Range("E2:E" & lRow)
For Each cel In MR
If cel.Value = "string" Then
If cel.Offset(, -1).Value = "" Then
cel.Offset(, -1).Interior.ColorIndex = 19
ElseIf cel.Offset(, -1).Value = "*" Then
cel.Offset(, -1).Interior.ColorIndex = -4142
End If
End If
Next
End Sub
If by contains "*" you mean "has any content" then:
If cell.Value = "X" Then
cell.Interior.ColorIndex = IIf(Len(cell.Offset(0, -1).Value) = 0, 19, xlNone)
End If

Resources