+ Reply to Thread
Results 1 to 6 of 6

Is there a way to write dynamic VBA code?

  1. #1
    Forum Contributor
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    680

    Is there a way to write dynamic VBA code?

    For example, there is database table (as attached screenshot), there is field ID =1, 2, 3, 4, 5, etc.... For each ID, it has a set of variable. Per screenshot example, ID=1 has 4 variable K1, K2, K6, K8; ID = 2 has 4 variable K1, K3, K5, K8

    For different ID, it can have same variable. The variable look same, but they are two different things. Assuming that a google employee and a facebook employee have exact same name, but they are two different person, same name does not mean anything.

    Now here is my question: I don't know what variable are under each ID. For example, for ID =1, I don't know how many variable and what they are, but I know to write VBA code to run a query and get those four variable K1, K2, K6, K8, then I can write below VBA code.

    ID is input data of form, when user choose a ID, the program should has its variables in the program.

    If user chooses ID = 1, the code should look like below:
    Please Login or Register  to view this content.
    Step 1: Write VBA to run query for variable where ID =1
    Step 2: There are four returned variable from step 1, how to Dim those four variable in the program. Above sample code is written manually after I run step 1 code. The question is how to write the code dynamically (automatically using the result from step 1)?

    Similarly, if user chooses ID = 2, the code should look like below:
    Please Login or Register  to view this content.
    I am not sure if I explain my question clearly. I have tried my best to make up an example table. I can explain more if it confuses you.

    Thanks.
    Attached Images Attached Images

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Is there a way to write dynamic VBA code?

    Your approach is going the wrong direction. You do not want to dynamically write code to create variables on the fly. What I suggest is that you create a Scripting.Dictionary. When the user chooses ID = 1 you load the dictionary using each "variable" as the key. Beyond that you haven't said what you want your code to do so I can't suggest a complete solution.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Is there a way to write dynamic VBA code?

    Why not using a 2-D array, to store ID and its relevant Variable?
    Like this:

    'declare array with maximum 10 ID's and maximun 100 Variable for each ID (adjust to actual number)
    ' this array with 1st column is ID, 2nd columns is for variables name, 3rd is its value; then next 4th+5th (for next variable);
    Dim ID(1 to 10, 1 to 100)
    ID(1,1) = 1
    ID(1,2) = "K1" :ID(1,3) = 1
    ID(1,4) = "K2" :ID(1,5) = 2
    ...

    ID(2,1) = 2
    ID(2,2) = "K1" :ID(2,3) = 5
    ID(2,4) = "K3" :ID(2,5) = 6
    ...
    Quang PT

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Is there a way to write dynamic VBA code?


    Hi,

    or just using a Type record structure …
    But if the subject is really about a database so maybe Excel is not the appropriate tool
    as far slower & unsecure and less efficient than a database software.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Is there a way to write dynamic VBA code?

    Quote Originally Posted by Marc L View Post
    or just using a Type record structure …
    But if the subject is really about a database so maybe Excel is not the appropriate tool
    If you have a specific suggestion it would be very helpful to show some code as a concrete example. It is not obvious how a Type record structure would solve this problem.

    Also many people use the word "database" to mean "some data on an Excel worksheet." The information in the OP is not enough to understand if this is just a table of data, or a true database.

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Is there a way to write dynamic VBA code?


    Yes it's just a suggestion according to 'database' and
    according to last 'condescending' moderator notice I could not write is yet explained in VBA help.
    And as any of my Excel versions works with a picture attachment …

+ 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. [SOLVED] Write a dynamic text based on 3 column
    By hessam.p in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-10-2019, 03:50 AM
  2. [SOLVED] Want to write a Dynamic R1C1 Formula using VBA with Dynamic Additions
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-25-2018, 06:42 AM
  3. [SOLVED] Populate Dynamic Array from Cell Values and write to Dynamic Range
    By TFiske in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2018, 09:09 AM
  4. Replies: 4
    Last Post: 08-27-2016, 06:24 AM
  5. [SOLVED] Macro to write a formula with dynamic cell references
    By alpha608 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-27-2013, 12:18 PM
  6. how to write excel macro for repeated dynamic data
    By knadt in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-05-2006, 10:35 AM
  7. Write a Listbox list to a dynamic range
    By blayne in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-29-2005, 01:40 PM

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