Problem with formula in VBA

rpagano

New Member
Joined
Jun 29, 2011
Messages
2
Hello all! This is my first post here.

I want to subtract Revenues from Expenses to find a P&L number. The code I used below is to find the row number where Revenues and Expenses are in the Excel file. The formula is where I am having trouble. The formula is being entered in cell H90.

Dim Rev As Integer
Dim Exp As Integer
Rev = Application.WorksheetFunction.Match("Revenues", Range("C:C"), 0)
Exp = Application.WorksheetFunction.Match("Expenses", Range("C:C"), 0)
Range("H90").Select
ActiveCell.Formula = "=H[Rev]-H[Exp]"

The formula is clearly wrong, but I am unsure how to reference those Match values in the formula. Any help would be appreciated.

Thanks,

Ryan
 

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.
Welcome to the board..

Try

ActiveCell.Formula = "=H" & Rev & "-H" & Exp


Also, you don't have to select the cell, and then use Activecell...
You can just put the formula in the cell directly..


Instead of
Range("H90").Select
ActiveCell.Formula = "=H" & Rev & "-H" & Exp

You can just do
Range("H90").Formula = "=H" & Rev & "-H" & Exp


Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,331
Members
452,907
Latest member
Roland Deschain

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