+ Reply to Thread
Results 1 to 13 of 13

vba dynamic value assignment

  1. #1
    Registered User
    Join Date
    11-21-2012
    Location
    Al Ain, UAE
    MS-Off Ver
    Excel 2003
    Posts
    16

    vba dynamic value assignment

    Hi,

    I have a combobox. Which has 4 values
    North, West, South, East.

    In a macro I want to get other values based on the selection.
    for example i have variables for flow.
    It is named as Flow_N, Flow_W, Flow_S, Flow_E.

    i want to assign the flow value to a temporary variable based on the selection.

    i did like this before for ole objects. but i dont know how to do it for variables.
    ActiveSheet.OLEObjects("Approach" & k).Object.Value


    Can anyone help me?

    Thanks

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: vba dynamic value assignment

    Maybe this? It's unclear what you want to store in the variables, a string? boolean?
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    11-21-2012
    Location
    Al Ain, UAE
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: vba dynamic value assignment

    Your code is fine.
    But do you have any other easy solution like the example i have given for ole objects.
    because i have 15 scenarios and 4 approaches.
    If i write your code i have to write 60 scenarios.
    I don't want that.

  4. #4
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: vba dynamic value assignment

    It's very difficult to visualize what you are trying to do. You need to explain more, what do you want to do with the variables? As for your example of OLE objects, it seems to me you are looping 'k' with numbers.

    Your initial question was, how to set a value into a variable based on the selection in the combo box.

    You need to explain now what are these "scenarios" or "approaches", or even better, post a sample workbook (with sensitive information removed) to explain what you want to achieve.

  5. #5
    Registered User
    Join Date
    11-21-2012
    Location
    Al Ain, UAE
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: vba dynamic value assignment

    'If I_Count > 0 Then
    '
    ' If t <= I_End(1) And t >= I_Start(1) Then
    '
    ' Flow_N(i) = Static_Input_Flow_Per_Lane
    '
    '
    ' If Down_block = 1 And Up_block = 0 Then
    ' 'Scenario = 1
    ' Speed_N(t, i) = Speed_N(t, i) * 5 / 6
    ' Flow_N(i) = Flow_N(i) * 1
    ' Cells(20, 14) = Flow_N(i)
    '
    ' ElseIf Down_block = 0 And Up_block = 1 Then
    ' 'Scenario = 2
    ' Speed_N(t, i) = Speed_N(t, i) * 2 / 3
    ' Flow_N(i) = Flow_N(i) * 3 / 2
    ' Cells(20, 14) = Flow_N(i)
    '
    ' ElseIf Down_block = 1 And Up_block = 1 Then
    ' 'Scenario = 3
    ' Speed_N(t, i) = Speed_N(t, i) * 2 / 3
    ' Flow_N(i) = Flow_N(i) * 3 / 2
    ' Cells(20, 14) = Flow_N(i)


    As You can see above,
    this is for for approach. That is for North.
    It has has 15 scenarios.

    I need to write the same for West, South and East.
    I can write it and use case or if/else and run.

    What I exactly want to do is, with the same code I want to run all.
    for example
    If North is selected,

    "Flow_" & Incident_Location.Value & "(" & i & ")" = Static_Input_Flow_Per_Lane
    similarly for speed
    "Speed_" & Incident_Location.Value & "(t,i)"= "Speed_" & Incident_Location.Value & "(t,i)" * 2/3

    Where Incident_Location.Value is value from the combobox selected.

  6. #6
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: vba dynamic value assignment

    Since you are using arrays, maybe add another dimension to it to represent the approach?

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    11-21-2012
    Location
    Al Ain, UAE
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: vba dynamic value assignment

    In Speed_N, the N represent North. Its like Spped_N, Speed_W, Speed_S and Speed_E.
    I cannot change the array as it is used in many other places.

    Is there any other way to do it?

    Somthing like this

    "Speed_" & Left(Incident_Location.Value,1) & "(t, i)" = "Something"

  8. #8
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: vba dynamic value assignment

    I looked up a little, and found this:
    The simple answer is, No.

    You will have to change your variables into an array or a collection for this to work. Or hidden named ranges in Excel, or properties of an object, or text files... lots of alternatives, except the simplest one that you want.

    I'm afraid that's not possible with variable names. Or maybe someone else can shed some light on this.

  9. #9
    Registered User
    Join Date
    11-21-2012
    Location
    Al Ain, UAE
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: vba dynamic value assignment

    Is it possible for me to get the address of array?

    That is where Speed_N is stored?

  10. #10
    Registered User
    Join Date
    11-21-2012
    Location
    Al Ain, UAE
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: vba dynamic value assignment

    like pointers in C.

  11. #11
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: vba dynamic value assignment

    As far as I know, it's not possible. Maybe some hardcore programming wizard might be able to help though. Anyway, good luck with that.

  12. #12
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: vba dynamic value assignment

    Can't you just use a collection?

  13. #13
    Registered User
    Join Date
    11-21-2012
    Location
    Al Ain, UAE
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: vba dynamic value assignment

    What is collection? Can u explain?

+ 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. VBA Assignment HELP ME!!
    By shell.clam in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-09-2012, 12:45 AM
  2. new assignment - need help
    By ivykikoe in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-26-2012, 05:40 AM
  3. i need help with my assignment
    By freakzilla in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-25-2006, 01:37 PM
  4. Assignment Help?
    By owensmorgan in forum Excel General
    Replies: 2
    Last Post: 11-08-2005, 04:17 AM
  5. [SOLVED] Key Assignment Log
    By Norma in forum Excel General
    Replies: 0
    Last Post: 05-12-2005, 12:06 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