in VBA, enter formula for product

Alan_CT06

New Member
Joined
Nov 6, 2023
Messages
16
Office Version
  1. 2016
Platform
  1. Windows
I have the following spreadsheet

A B C D

1 Issuer Coupon Notional Interest
2 Ford 6.00 1,000,000
3 GM 6.50 2,000,000
4 MSFT 7.00 3,000,000

I would like to enter a formula in column D that will multiply column B * Column C * .01

I am able to create variable for the ranges of the Coupon and the Notional and believe there is some way I can enter formulas for the whole range of Interest (D2:D4) that would multiply B*C*.01.

Below is my code so far.

Thank you for your help.



VBA Code:
Sub AnnualInterest()

    Set LastRow = Cells.Find(What:="Issuer").End(xlDown)

    Set FirstCoupon = Cells.Find(What:="Coupon").Offset(1)
    Set Coupon = Range(FirstCoupon, Cells(LastRow.Row, FirstCoupon.Column))
   
    Set FirstNotional = Cells.Find(What:="Notional").Offset(1)
    Set Notional = Range(FirstNotional, Cells(LastRow.Row, FirstNotional.Column))
   
    Set FirstInterest = Cells.Find(What:="Interest").Offset(1)
      
    Set Interest = Range(FirstInterest, Cells(LastRow.Row, FirstInterest.Column))
   

End Sub
 
Last edited by a moderator:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Welcome to the MrExcel board!

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

I suggest that you investigate XL2BB for providing sample data & expected results to make it easier for helpers to understand just what you have & where it is and also what you want & where it is to be.

Does this do what you want? Test with a copy of your workbook.

VBA Code:
Sub Insert_Formula()
  Range("D2:D" & Range("C" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=RC[-2]*RC[-1]*.01"
End Sub
 
Upvote 0
Welcome to the MrExcel board!

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

I suggest that you investigate XL2BB for providing sample data & expected results to make it easier for helpers to understand just what you have & where it is and also what you want & where it is to be.

Does this do what you want? Test with a copy of your workbook.

VBA Code:
Sub Insert_Formula()
  Range("D2:D" & Range("C" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=RC[-2]*RC[-1]*.01"
End Sub
Peter,

Thank you for the reply and I will try the XL2BB next time.

One questions, in case I move my columns, is there a way I can modify this to reference the column by the range name, so rather than
"=RC[-2]*RC[-1]*.01", using the [-2] and [-1]

the code would define the column by the Notional and Coupon ranges defined in my code?
 
Upvote 0
in case I move my columns, is there a way I can modify this to reference the column by the range name, so rather than
"=RC[-2]*RC[-1]*.01", using the [-2] and [-1]

the code would define the column by the Notional and Coupon ranges defined in my code?
Sure. This is not quite how I would do it if starting from scratch, but using your existing code as a basis, try this.
I have commented out some of the code as it is not needed for this particular task, though you may still want it for something else that might be in your code.

VBA Code:
Sub AnnualInterest_v2()
  Dim LastRow As Range, FirstCoupon As Range, FirstNotional As Range
  Dim FirstInterest As Range, Interest As Range
'  Dim Coupon As Range, Notional As Range
  
   Set LastRow = Cells.Find(What:="Issuer").End(xlDown)

   Set FirstCoupon = Cells.Find(What:="Coupon").Offset(1)
'   Set Coupon = Range(FirstCoupon, Cells(LastRow.Row, FirstCoupon.Column))
  
   Set FirstNotional = Cells.Find(What:="Notional").Offset(1)
'   Set Notional = Range(FirstNotional, Cells(LastRow.Row, FirstNotional.Column))
  
   Set FirstInterest = Cells.Find(What:="Interest").Offset(1)
     
   Set Interest = Range(FirstInterest, Cells(LastRow.Row, FirstInterest.Column))
   
   Interest.Formula = "=" & FirstCoupon.Address(0, 0) & "*" & FirstNotional.Address(0, 0) & "*0.01"

End Sub
 
Upvote 0
Solution
Sure. This is not quite how I would do it if starting from scratch, but using your existing code as a basis, try this.
I have commented out some of the code as it is not needed for this particular task, though you may still want it for something else that might be in your code.

VBA Code:
Sub AnnualInterest_v2()
  Dim LastRow As Range, FirstCoupon As Range, FirstNotional As Range
  Dim FirstInterest As Range, Interest As Range
'  Dim Coupon As Range, Notional As Range
 
   Set LastRow = Cells.Find(What:="Issuer").End(xlDown)

   Set FirstCoupon = Cells.Find(What:="Coupon").Offset(1)
'   Set Coupon = Range(FirstCoupon, Cells(LastRow.Row, FirstCoupon.Column))
 
   Set FirstNotional = Cells.Find(What:="Notional").Offset(1)
'   Set Notional = Range(FirstNotional, Cells(LastRow.Row, FirstNotional.Column))
 
   Set FirstInterest = Cells.Find(What:="Interest").Offset(1)
    
   Set Interest = Range(FirstInterest, Cells(LastRow.Row, FirstInterest.Column))
  
   Interest.Formula = "=" & FirstCoupon.Address(0, 0) & "*" & FirstNotional.Address(0, 0) & "*0.01"

End Sub
Yes, that works perfectly, thank you.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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