+ Reply to Thread
Results 1 to 10 of 10

Update vba code through user input.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Update vba code through user input.

    I think I am just hoping for an easy way out but is it possible to have self updating code based on user input?

    I have a form that includes a combo box. As an example lets say I had this...

    
    Sub frm_Mode_Initialize()
        'Clear previous values.
        frm_Mode.cmbBox.Clear
        With frm_Mode.cmbBox
            .AddItem "blue"
            .AddItem "red"
            .AddItem "yellow"
        End With
        frm_Mode.Show
    End Sub
    where frm_Mode would allow the user to pick blue, red, yellow, or enter a new value. If the user entered a new value could I add add this to the initialize list for future use? OR will I have to keep track of this in a hidden spreadsheet or something similar?

    TL:DR -> I am pretty sure I am expected to much out of VBA but it would be reassuring to know self updating code existed.
    Last edited by 111StepsAhead; 06-01-2011 at 10:20 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Update vba code through user input.

    Hi,

    Does John Walker's web site http://j-walk.com/ss/excel/tips/tip61.htm help?

    I can't help thinking it will be easier/simpler to write the new values to a dynamic range name on a hidden and password protected sheet if necessary, and then using the range name in the row source property.

    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Update vba code through user input.

    The 112th step..

    Sub Userform_Initialize()
      cmbBox.list=sheets(1).cells(1,10).currentregion.columns(1).value
    End Sub
    The initialize event will be triggered when:
    frm_Mode.show
    If you want a connection between the combobox and userinput in the worksheet:

    Private Sub cmbBox_click()
      cmbBox.list=sheets(1).cells(1,10).currentregion.columns(1).value
    end sub



  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Update vba code through user input.

    This is how I would do it, store the list on Sheet1, which can be very hidden.

    The code in the combobox exit event checks if the entry is listed, if not there's an option to add it to the list
    Attached Files Attached Files
    Last edited by royUK; 06-01-2011 at 10:16 AM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: Update vba code through user input.

    For today I think I'll stick with Row source but later this evening I am going to dive into the link you gave me. My question about RowSource is... Is it possible to use a range that is a row?

    I have this

    
    Private Sub SetCombo()
    frm_Mode.cmbBox.RowSource = "Mode_Tracker_0001!A1:C1"
    End Sub
    Which will only add the item in A1 to my combo box.

    However, If I have

    
    Private Sub SetCombo()
    frm_Mode.cmbBox.RowSource = "Mode_Tracker_0001!A1:A3"
    End Sub
    Suddenly the combo box will have 3 items; one for each cell in the range.

    What is going on? I noticed Excel doesn't have a ColumnSource so once again am I just out of luck here?

    EDIT: I should have refreshed the page before posting...

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Update vba code through user input.

    Hi,

    Yes that's indeed the case, your combobox will contain as many columns as there is in the Row Source range.

    If you only want to see one column in the combobox then you can of course either limit the rowsource range or use .Resize to shrink a named range down.

    However having all columns is often very useful. The usual way to 'supress' columns is to use the ColumnWidths property to hide them. So for instance if there are 3 columns in the combobox and you want to see only the 2nd column set the ColumnWidth property to say 0;30;0. To see the 3rd column 0;0;30. Obviously change the 30 to widen or narrow what you see.

    Don't also forget that you have the BoundColumn property which determines which column of the combobox gives the value. You can therefore have the combo box present say the first column by using the ColumnWidths property as described above and set the BoundColumn property to say 2, so that the combobox uses the 2nd column for its Value.

    HTH

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Update vba code through user input.

    Why are using a horizontal list & why 3 Columns in the ComboBox?

  8. #8
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: Update vba code through user input.

    This is possibly a case of the x-y problem. http://www.encyclo.co.uk/define/XY%20problem

    I wanted to create a sheet where Row 1 would be the different modes.
    The cells under each mode (or each column) would represent the different sheets to display. (The other sheets would be hidden.)

    So if a user selects a mode the certain sheets appear. This way I can combine different applications used at my office into one excel sheet that is user friendly.

    In the example workbook I posted you see Row1 has modes 'blue', 'red', and 'green'. Each mode has 3 sheets associated with it. A user form lets you pick the mode and then those sheets appear while the other 6 would stay hidden. If the mode was switched the other 3 sheets would appear.

    Although in my example I suppose I could make 4 columns. The first for the mode and a column then for each mode.
    However, it turns out I am having trouble with simply initializing my combo box on the first user form even when using a vertical list.
    Is it true that when I say frm_Mode.show that frm_Mode_Initialize automatically takes place? Because my code apparently skips over it.
    I call frm_Mode.Show from Sheet1 where frm_Mode_Initialize is in the user form code.

    EDIT: I got it to work with vertical lists. Sort of an indirect way (putting list into an array and adding each array item to combobox) but as of right now I need to move on. I spent too much time this morning figuring this out. Thanks for all your help.
    Attached Files Attached Files
    Last edited by 111StepsAhead; 06-01-2011 at 11:48 AM.

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Update vba code through user input.

    Where's the example workbook?

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Update vba code through user input.

    Thanks for the rep, but your comment mentioned implementing a search to prevent duplicates. Check the code it's already in there.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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