Help Fix my VBA Code

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
144
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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
2,147
Office Version
  1. 2013
Platform
  1. Windows
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?
 

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
144
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.
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
2,147
Office Version
  1. 2013
Platform
  1. Windows
You prefer VBA over a formula?
 

cmschmitz24

Board Regular
Joined
Jan 27, 2017
Messages
144

ADVERTISEMENT

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.
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
2,147
Office Version
  1. 2013
Platform
  1. Windows
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 )
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
63,990
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
63,990
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback
 

Forum statistics

Threads
1,144,389
Messages
5,724,073
Members
422,534
Latest member
ThiccNugg

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
Top