+ Reply to Thread
Results 1 to 11 of 11

Increment Variable from User Form Selection

Hybrid View

jonhfl Increment Variable from User... 04-01-2010, 09:59 AM
davegugg Re: Increment Variable from... 04-01-2010, 10:29 AM
jonhfl Re: Increment Variable from... 04-01-2010, 10:31 AM
davegugg Re: Increment Variable from... 04-01-2010, 10:44 AM
jonhfl Re: Increment Variable from... 04-01-2010, 10:48 AM
jonhfl Re: Increment Variable from... 04-01-2010, 10:51 AM
davegugg Re: Increment Variable from... 04-01-2010, 10:54 AM
jonhfl Re: Increment Variable from... 04-01-2010, 10:58 AM
jonhfl Re: Increment Variable from... 04-01-2010, 11:03 AM
davegugg Re: Increment Variable from... 04-01-2010, 11:17 AM
jonhfl Re: Increment Variable from... 04-01-2010, 11:29 AM
  1. #1
    Registered User
    Join Date
    04-01-2010
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2007
    Posts
    36

    Increment Variable from User Form Selection

    Hi,

    I have a workbook that when opened displays a user form with two buttons. Each button goes to a different worksheet. Basically it gives the option of which worksheet you want to start with. I have a MsgBox that displays with instructions after you make your selection. Then on the sheet you open there is a button to go to the other sheet. I am trying to have that button prompt a MsgBox if the other sheet has not yet been selected. I can get excel to recognize the sheet has been selected once it is toggled to from the other sheet however when I go back to the first sheet it thinks it is the first time. I tried adding code to the form button on_click to increment an occurance variable I have but that does not seem to store whe nthe form is unloaded. Also the occurance variable is declared to the workbook so not within a Sub. Any help?

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Increment Variable from User Form Selection

    Did you try declaring the occurance variable as Global?
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Registered User
    Join Date
    04-01-2010
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: Increment Variable from User Form Selection

    I believe so. It is declared above any sub procedure in the workbook. Is that what you mean?

  4. #4
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Increment Variable from User Form Selection

    It should say:

    Global strVariable As String
    in a standard module.

  5. #5
    Registered User
    Join Date
    04-01-2010
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: Increment Variable from User Form Selection

    Here is the code, I will try to break it out so you can see the steps....

    Dim Occ2Num as Integer - Occ2Num counts how many times Sheet("Model") is viewed
    Dim Occ3Num as Integer - Occ3Num counts how many times Sheet("Regional") is viewed
    
    Private Sub Workbook_Open()
    
    
        MainSelection.Show
    
    End Sub
    
    Private Sub RunComp_Click()
    
    Occ2Num = Occ2Num + 1
    
    Unload Me
    
    MsgBox ("To begin use the highlighted input cells to select a Base, Comp 1 and Comp 2 property.  Or see the Comp Chart to view comparable properties and make selections.")
    
    
    
    End Sub
    
    Private Sub RunReg_Click()
    
    Worksheets("Regional").Select
    Range("A1").Select
    
    Occ3Num = Occ3Num + 1
    
    Unload Me
    
    MsgBox ("To begin use the highlighted cells to select a Company, Region and Unit Size and then Press the 'Process Request' button.")
    
    MsgBox ("A property may have more than one unit type of a selected size.  Use the drop downs below the Association name to view other unit types of the same size.")
    
    
    End Sub
    
    At this point you are now in one of the two sheets.  The next two parts navigate from one sheet to the other once the workbook is loaded.
    
    Sub toregional()
    
        If Occ3Num = 0 Then
        Worksheets("Regional").Select
        Range("A1").Select
        
        MsgBox ("To begin use the highlighted cells to select a Company, Region and Unit Size and then Press the 'Process Request' button.")
        
        MsgBox ("A property may have more than one unit type of a selected size.  Use the drop downs below the Association name to view other unit types of the same size.")
    
        Occ3Num = Occ3Num + 1
    
        Else
        Worksheets("Regional").Select
        Range("A1").Select
        
        End If
        
    End Sub
    
    Sub tomodel()
    '
        If Occ2Num = 0 Then
        Sheets("Model").Select
        Range("A1").Select
            MsgBox ("To begin use the highlighted input cells to select a Base, Comp 1 and Comp 2 property.  Or see the Comp Chart to view comparable properties and make selections.")
        Occ2Num = Occ2Num + 1
        
        Else
        Sheets("Model").Select
        Range("A1").Select
        
        End If
        
    End Sub

    So it works perfect except for the fact that the one you select in the form will prompt again when navigated to via the macro. But it will only do it one more time.
    Last edited by jonhfl; 04-01-2010 at 10:56 AM.

  6. #6
    Registered User
    Join Date
    04-01-2010
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: Increment Variable from User Form Selection

    I tried changing where I have

    Dim Occ2Num as Integer
    Dim Occ3Num as Integer
    to
    Global Occ2Num as Integer
    Global Occ3Num as Integer
    Same results
    Last edited by jonhfl; 04-01-2010 at 10:57 AM.

  7. #7
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Increment Variable from User Form Selection

    Before I can continue to help you, you are going to need to edit your above two posts to include code tags. See the forum rules.

    But: You would want to keep the same variable type: integer, not string.

  8. #8
    Registered User
    Join Date
    04-01-2010
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: Increment Variable from User Form Selection

    Sorry I did keep them as Integers. Still does not seem to recognize the increase to the variable from the form.

  9. #9
    Registered User
    Join Date
    04-01-2010
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: Increment Variable from User Form Selection

    I got it to work.... I replaced the code with the +1 to call a sub routine to do the increment and it worked.

    Private Sub RunReg_Click()
    
    Worksheets("Regional").Select
    Range("A1").Select
    
    Call Occ3Numup
    
    Unload Me
    
    MsgBox ("To begin use the highlighted cells to select a Company, Region and Unit Size and then Press the 'Process Request' button.")
    
    MsgBox ("A property may have more than one unit type of a selected size.  Use the drop downs below the Association name to view other unit types of the same size.")
    
    
    End Sub
    
    
    Sub Occ3Numup()
    
    Occ3Num = Occ3Num + 1
    
    End Sub
    I had tried that earlier but I think it was the global that solved it.

    Thank you for the help.
    Last edited by jonhfl; 04-01-2010 at 11:10 AM. Reason: Solved

  10. #10
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Increment Variable from User Form Selection

    I think you should remove the message boxes from inside the RunComp_Click() and
    RunReg_Click() procedures. Does that work?

  11. #11
    Registered User
    Join Date
    04-01-2010
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: Increment Variable from User Form Selection

    I guess I could actually just remove everything from teh _Click and just call the Sub? That would clean it up a bit and I think do the same thing. It is working now so I will do a bit of clean up.

+ 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