Help Fix my VBA Code

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
150
Hello, I'm trying to look up a value in column H, then return a value in column V, from a formula specific to what is in column H and column S.

Here is my current VBA code that will not produce what I'm looking for.

Sub Premiums()

Dim Lastrow As Long
Dim myRow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For myRow = 2 To Lastrow

Select Case Left(Cells(myRow, "H"), 1)
Case "A": result = ActiveCell.FormulaR1C1 = "=RC[-3]/12"
Case "C": result = ActiveCell.FormulaR1C1 = "=RC[-3]/12"
Case "H": result = ActiveCell.FormulaR1C1 = "=RC[-3]/24"
Case Else: result = ""
End Select

Cells(myRow, "V") = result
Next myRow

End Sub

The returned value says "FALSE", so I'm thinking the result code is not correct.

Thanks,
Christina
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Your code depends on the active cell, so the outcome is unpredictable.
Is it possible for you to paste a fragment of your worksheet and your required outcome?
 
Upvote 0
1584476550917.png


The values in column H can change, however they will always start with either A, C, or H. Based on those, I need the formula to populate the correct value in column V.
If A or C, then take the corresponding value in column S and divide by 12. If H, then take the corresponding value in column S and divide by 24.
 
Upvote 0
You prefer VBA over a formula?
 
Upvote 0
I guess I don't care, to be honest.
But I wasn't able to understand how to write the if/then statement to only look at the first letter of column H.
 
Upvote 0
Try this formula for the cells in column V, starting in row 2 and then copying that cell downwards:
=IF(OR(LEFT(H2,1)="A",LEFT(H2,1)="C"), S2/12, S2/24 )
 
Upvote 0
With your code
VBA Code:
Sub Premiums()

   Dim Lastrow As Long
   Dim myRow As Long
   Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
   For myRow = 2 To Lastrow
   
      Select Case Left(Cells(myRow, "H"), 1)
         Case "A", "C": Cells(myRow, "V").FormulaR1C1 = "=RC[-3]/12"
         Case "H": Cells(myRow, "V").FormulaR1C1 = "=RC[-3]/24"
         Case Else: Cells(myRow, "V") = ""
      End Select
   
   Next myRow

End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,242
Members
448,951
Latest member
jennlynn

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