VBA using IF OR THEN statements and returning the product of cells in two columns.

ssmith3156

New Member
Joined
Apr 11, 2023
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I need to write a VBA code multiplies the cells in columns J & K to return a result in column L, if the value in the cell of column H is L or O. For example: Since H21=L, then L21=J21*K21. The columns will never change, but the rows will change every month. This is what I have written, but it is not working.

Sub LaborCost()
'
' LaborCost Macro
' Enters formula to calculate cost for L and OT cost types.
'

'
Sheets("TOTAL COSTS").Select
Dim LastRow As Long
Dim i As Long
LastRow = Range("H" & Rows.Count).End(xlUp).Row

For i = 2 To LastRow
If Range("H" & i).Value = L Or Range("H" & i).Value = O Then
Range("L" & i).Value = PRODUCT("J" & i:"K" & i)

End If
Next i
Range("A2").Select
End Sub


1681240471751.png
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi @ssmith3156. Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.


Try this:
VBA Code:
Sub LaborCost()
  ' LaborCost Macro
  ' Enters formula to calculate cost for L and OT cost types.
 
  Dim i As Long
  Sheets("TOTAL COSTS").Select
  For i = 2 To Range("H" & Rows.Count).End(xlUp).Row
    If Range("H" & i).Value = "L" Or Range("H" & i).Value = "O" Then
      Range("L" & i).Value = Range("J" & i).Value * Range("K" & i).Value
    End If
  Next i
End Sub

--------------
Or this:

VBA Code:
Sub LaborCost_2()
  Sheets("TOTAL COSTS").Select
  With Range("L2:L" & Range("H" & Rows.Count).End(xlUp).Row)
    .Formula = "=IF(OR(H2=""L"",H2=""O""),J2*K2,"""")"
    .Value = .Value
  End With
End Sub

-------------------------------

Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Last edited:
Upvote 1
Solution

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top