Apply formula to variable

roro

New Member
Joined
Nov 9, 2004
Messages
8
I am looking to run a formula and instead of returning a result within a cell, have it return the result in a variable I have declared. Example below:
I am trying to match a value and then return that value into my variable "totrow". The formula works if I attach it to a cell, but the syntax is wrong if I try to use it in VBA.

Dim totrow As String
totrow = MATCH(A1,Sheet1!A1:A13,0)

BTW, thanks to all those that take the time to respond to all us newbies, you make this an awesome forum and provide so much help to us. Much appreciated!!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Generally, you have to do something like:
Code:
Application.WorksheetFunction.Match....
Please note that the referencing process changes slightly. HTH.
 
Upvote 0
Tried this but it gives me a syntax error still.
Code:
totrow = Application.WorksheetFunction.Match(A1,A1:A13,0)
 
Upvote 0
I found the problem. I had to tell VB that it was a range. That was the difference in using it in VB versus physically applying it to a cell.
Code:
totrow = Application.WorksheetFunction.Match(Range("A2"), Range("SAP!A1:A13"), "0")
 
Upvote 0

Forum statistics

Threads
1,203,326
Messages
6,054,744
Members
444,748
Latest member
knowak87

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