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?:eek:
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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,215,590
Messages
6,125,707
Members
449,251
Latest member
Herushi

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