Build VBA expression from a string

Bryhni

New Member
Joined
Nov 15, 2012
Messages
7
Dear all, it's late at night where I am and my brain is not working anymore (if it ever was): I want to build and evaluate a VBA expression by reading in a string with a VBA expression from a spreadsheet cell. The purpose is to match up tags based on recognized patterns. Given a tag pattern is recognized, I want to see if I can manipulate the string until I can find a match in a range. Example: ' Cell A1 contains the string "Mid(Tag,7,12) & Right(Tag)" and Tag is a string variable used in the VBA Function. How can I evaluate a TestTag based on this string:
Code:
TestTag = Evaluate(Range("A1").Value)
... obviously doesn't work. If I coded all the different variants into the VBA code it would look like this:
Code:
TestTag = Mid(Tag,7,12) & Right(Tag)
.... which obviously does work. But it does not provide the clarity of a user-accessible table with all patterns and manipulation options laid out in a sheet. Any hints?:eeek:
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I think it is possible without the variables, but I'm not sure how to do it with the variable "Tag" in there.
 
Upvote 0
Thanks, djreiswig! You nudged me in the right direction: By replacing the variable "Tag" with the value of Tag, a string, the expression became purely "formula" and could be evaluated:
Code:
TestPattern = Evaluate(Range("A1").Value) TestTag = Replace(TestPattern, "Tag", Chr(34) & Tag & Chr(34) TestTag=Evaluate(TestPattern)
Probably not the most elegant code, but it works.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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