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

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.

Related

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.

vba if condition works fine but misbehaves when else statement is added

I have this code below.
Sub workbook_initialize()
Dim cell As Excel.Range
Dim LastRow As Long
LastRow = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row
For Each cell In Sheets("sheet1").Range("E1:E" & LastRow)
For i = 1 To Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Row
If cell.Value >= Sheets("Sheet2").Cells(i, 8).Value And cell.Value _
<= Sheets("Sheet2").Cells(i, 11).Value Then
Sheets("Sheet1").Cells(cell.Row, 10).Value = _
Sheets("Sheet2").Cells(i, 3).Value
End If
Next i
Next
End Sub
What the code does is loop through a particular range of cells in Sheet1 and if any cell has value greater than or equal to value in Sheet2 Column H and at the same time less than or equal to the value of Sheet2 Column K on same row then make Sheet1 Column J same value as the corresponding cell in Sheet2 Column C, which works fine but when I add this line of code below, it doesnt yield the expected result.
Else
Sheets("Sheet1").Cells(cell.Row, 10).Value = "No Shift"
I'll appreciate any help I can get.
From the code flow and the description given, I assume you want to Check each value of Sheet1 Col E comes within in the limit defined by Sheet2 col H & K and a corresponding token value indicating the matching range is to picked up from Sheet2 Col 3 and to be placed in Sheet 1 col 10. If sheet1 Col e value does not satisfy to any of the ranges within all the ranges listed in sheet 2 “No shift" is to be indicated.
With this code format it is better not to use else here since you have to complete the inner for loop to check all the limit values for testing the conditions and then only “No shift” condition be satisfied.
I tweaked your code a little. Please try
Dim cell As Excel.Range
Dim LastRow As Long, found As Boolean
LastRow = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row
For Each cell In Sheets("sheet1").Range("E1:E" & LastRow)
found = False
For i = 1 To Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Row
If cell.Value >= Sheets("Sheet2").Cells(i, 8).Value And cell.Value _
<= Sheets("Sheet2").Cells(i, 11).Value Then
Sheets("Sheet1").Cells(cell.Row, 10).Value = _
Sheets("Sheet2").Cells(i, 3).Value
found = True
Exit For
End If
Next i
If found = False Then Sheets("Sheet1").Cells(cell.Row, 10).Value = "No Shift"
Next

VBA marking cells Based on keywords

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

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