+ Reply to Thread
Results 1 to 9 of 9

Code to run a string as code.

  1. #1
    Forum Contributor
    Join Date
    04-02-2017
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    403

    Code to run a string as code.

    Hi Guys,

    below is the code I have created.
    The code below should work.. IF there is a way of running a string within VBA as code.
    I found the "Sub StringExecute(s As String)" online and had hoped that it would work... it does not.

    Does anyone know how I can run my "Str02" variable as a piece of code (Str01 is a variable within the code).
    Thanks!

    Please Login or Register  to view this content.
    Last edited by JimmyWilliams; 10-09-2021 at 06:50 AM.
    Thanks,

    JimmyWilliams

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,865

    Re: Code to run a string as code.

    Rather than try to understand what you are trying to do with code that does not work for what you want, why not explain in simple English what you wish to happen. Read the yellow banner at the top of this page.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,384

    Re: Code to run a string as code.

    In what way does it not work? You have two End Sub statements together so I’d be surprised if it would compile, let alone work.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Contributor
    Join Date
    04-02-2017
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    403

    Re: Code to run a string as code.

    Quote Originally Posted by TMS View Post
    In what way does it not work? You have two End Sub statements together so I’d be surprised if it would compile, let alone work.
    Ok, I fixed that, but that wasnt the problem i was trying to fix.
    thanks anyways.

  5. #5
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    365 32bit Win
    Posts
    1,909

    Re: Code to run a string as code.

    Maybe something like this?

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    04-02-2017
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    403

    Re: Code to run a string as code.

    Quote Originally Posted by alansidman View Post
    Rather than try to understand what you are trying to do with code that does not work for what you want, why not explain in simple English what you wish to happen. Read the yellow banner at the top of this page.
    Hi Alan,


    I want to make a button connected to a macro that updates a pivot table with today's (or yesterday's data)
    see below code:
    Please Login or Register  to view this content.
    I need the "20211009" (YYYYMMDD) to change to today's date, (or yesterday's date) which I have coded in.


    Aka (tomorrow) I will need the macro to run the following code on the 10th of Oct 2021.
    Please Login or Register  to view this content.
    "Str02" equals the above code, however it is a string and I need VBA to run this string as a piece of code, not as a string.
    Last edited by JimmyWilliams; 10-09-2021 at 06:44 PM.

  7. #7
    Forum Contributor
    Join Date
    04-02-2017
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    403

    Re: Code to run a string as code.

    Please Login or Register  to view this content.



    That might work? I will try tomorrow at work to see if it does. (I'm not connected to the server, so I cannot update the data for testing)

    Actually... Now that I think about your code ByteMarks, I have a feeling it might not work because the code you suggested doesn't know which parts are strings and which parts are code.. this is why I need the Chr(34) to separate the strings and the code. (hope that makes sense).
    (I could be wrong, but I'm 99% sure what you've suggested won't work. I'll try tomorrow and let you know.)

    more or less I need:
    Please Login or Register  to view this content.
    where "Str02" can be ran as code somehow.
    Last edited by JimmyWilliams; 10-09-2021 at 07:28 AM.

  8. #8
    Forum Contributor
    Join Date
    04-02-2017
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    403

    Re: Code to run a string as code.

    Quote Originally Posted by ByteMarks View Post
    Maybe something like this?

    Please Login or Register  to view this content.
    Hey Bytemarks,

    What you suggested does work.
    Thanks

  9. #9
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    365 32bit Win
    Posts
    1,909

    Re: Code to run a string as code.

    Welcome

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 02-07-2020, 05:17 AM
  2. [SOLVED] Need help with VBA code. Tons of code seperated in two, second part of code doesn't work.
    By FragaGeddon in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 11-30-2015, 11:46 AM
  3. Storing complex text string into variable. Text string contains html or xml code.
    By jakopak in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-20-2015, 04:54 AM
  4. [SOLVED] Excel VB code. Message pops up while code running asking question. Code must not wait.
    By Heinrich Venter in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-28-2014, 06:10 AM
  5. [SOLVED] Need code to pull numeric data out of an alphanumeric string (string not constant)
    By harrydnyc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-09-2013, 04:44 PM
  6. Code to locate string or enter a new string with corresponding value
    By Yappa in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-18-2008, 07:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1