New to VBA but trying really hard to learn. I recently wrote a macro with IF Then Else for a woman at work. Unfortunately she is not as computer savvy and doesn't want to find the macro in the list so I figured I would use a command button user form and adjust the code to loop through the cells for her rather than her going from active cell to active cell. Sadly, I am having issues with the If Then Else in the For each loop.
Here was the original non-looping coded macro
Sub InvoicesTotalDue()
Dim myRange As Range
Set myRange = Application.InputBox(Prompt:="Select an Invoice Type Then Hit Enter", Type:=8)
'person is on active cell and selects cell in Type column of corresponding row of active cell
If myRange.Cells.Value = "Cancel300" Then
ActiveCell.Value = 300
ElseIf myRange.Cells.Value = "Cancel350" Then
ActiveCell.Value = 350
Else
ActiveCell.FormulaR1C1 = "=Product(100,[@Hours])"
End If
End Sub
This was working but the user found it tedious and she is not used to macro usage. So I figured a userform command button that looped through the range and filled in the corresponding values in the final column would be best for her. This the way I figured it should work.
Sub InvoicesTotalDue()
Dim myRange As Range
Set myRange = Application.Range("Invoices[Type]")
For each cCell in myRange
If myRange.Cells.Value = "Cancel300" Then
[TotalDue].Cell.Value = 300
ElseIf myRange.Cells.Value = "Cancel350" Then
[TotalDue].Cell.Value= 350
Else
[TotalDue].Cell.FormulaR1C1 = "=Product(100,[@Hours])"
End If
Next cCell
End Sub
Should calculate a total due based on type of invoice. Cancellations are set values, anything not a cancellation is the product of 100 and number of hours worked.
Can anyone spot my problem or have any suggestions/solutions?
Thanks!