Defining Variables

Jenawade

Board Regular
Joined
Apr 8, 2002
Messages
231
My macro opens a couple of different workbooks and enters a formula or two just to collect the results and use them in the workbook where the macro resides. Below is one of them.

Code:
    Windows("1.10.2.2.xls").Activate
    Range("K22").Formula = "=MAX(E4:E" & NoOfRowsA & ")"
    Range("L22").Formula = "=VLOOKUP(K22,E4:I" & NoOfRowsA & ",5,0)"
    CurrCost = Range("L22").Value
    CurrDate = Range("K22").Value

How can I eliminate the Range().Formula for the above? I don't know how to word this, but I'd like to (put/calculate?) the data right in the variable, something like this:

Code:
    CurrCost = "=VLOOKUP(K22,E4:I" & NoOfRowsA & ",5,0)"
    CurrDate = "=MAX(E4:E" & NoOfRowsA & ")"
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi Jen

Try (both not tested):

Code:
CurrCost = Evaluate("=VLOOKUP(K22,E4:I" & NoOfRowsA & ",5,0)")

or use the WorksheetFunction

Code:
CurrCost = Application.WorksheetFunction.VLookup(Range("K22"), Range("E4:I" & NoOfRowsA), 5, 0)
 
Upvote 0
Hi Jen

Try (both not tested):

Code:
CurrCost = Evaluate("=VLOOKUP(K22,E4:I" & NoOfRowsA & ",5,0)")

or use the WorksheetFunction

Code:
CurrCost = Application.WorksheetFunction.VLookup(Range("K22"), Range("E4:I" & NoOfRowsA), 5, 0)

Thanks for the quick response! I'm making a lot of big changes to the entire macro today so I'm just going to copy/paste the first one right in for now and test it tomorrow.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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