Consulting

Results 1 to 11 of 11

Thread: Solved: Problem Moving Code from Module to UserForm

  1. #1

    Solved: Problem Moving Code from Module to UserForm

    Originally, I created an application workbook at home using Excel 2003 on a Windows XP computer. It worked great when I took it to the office using Excel 2003 on a Windows NT 2002. However, it blew-up when I added a userform in the office and called some of the already working subs from one of the modules. I thought it might be a compatibility problem. Now I am not sure...

    Is there a documented problem in this area?

    Unfortunately, I cannot post a coy of the workbook because it is company sensitive and contains a lot of personal information.
    ttfn

    Kicker

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Thanks for noticing....
    Probably not a compatability problem......blew up???? Seriously what really happened...what was the error, etc.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Sorry about sounding frustrated.

    It just froze up on me. It appeared to be in a loop of some sort because I couldn't move the curser except in very very slow jerky motion. Could not get to the windows status bar to open the task manager. Finally opened task manager before I ran the routine and could barely get to it using alt-tab. In the task manager, it took 30 or 40 seconds before it would highlight Excel (which was identified as running) and then another 30 seconds to get it to stop. Once I had to unplug the computer. Never did get an error message.

    I have written dozens of routines at home and taken them to the office. Everyone of the worked fine.

    When called as a macro, this one works perfectly. When called from a user form or from another macro, it just throws up. Leads me to think. I think I will try to put a command button on the worksheet itself and see what happens.
    ttfn

    Kicker

  4. #4
    Well....it worked when I put a command button on the Exel worksheet.

    I am going to continue this way and hopefully can get it to work through a user form.
    ttfn

    Kicker

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Would have to have more info on the macro to help you any more....hope you get it worked out....if not post back here.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    OK. I stripped most everything out of the file except what is needed for the sub to run. Changed the names to protect the innocent.

    CAUTION....CAUTION....This WILL freeze your computer. Suggest you open it up and read all the macros first.

    There are 3 macros.

    From the Macros/run menu.
    1. Show menu opens a user form
    2. TeamSC runs
    3. TeamSOS runs

    On the Teams worksheet, the command button works to run the macros

    On the userform, it goes wacky.
    ttfn

    Kicker

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Send Keys is not a good method of coding unless there is no alternative. In this case it is not changing the Active cell as you intend, so this is looping endlessly. I added in tmp for debugging
    [vba]
    While Not ActiveCell.Value = ""
    Dim tmp
    tmp = ActiveCell.Address
    SendKeys "{DOWN}", True
    Wend
    [/vba]
    Try [VBA]While Not ActiveCell.Value = ""
    ActiveCell.Offset(1).Activate
    Wend[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Not sure what is going on but it's got something to do with the lines in red. If you comment them out for the first button it goes ok.
    [vba]Sub team_SOS()
    Dim r As Long
    Dim rr As Long
    Dim n As Long

    Sheets("SecResources").Select
    Selection.AutoFilter Field:=2
    Selection.AutoFilter Field:=3
    For n = 1 To 3
    Sheets("SecResources").Select
    Selection.AutoFilter Field:=3, Criteria1:="SOS"
    Select Case n
    Case Is = 1
    Selection.AutoFilter Field:=2, Criteria1:="=D?", Operator:=xlAnd
    Case Is = 2
    Selection.AutoFilter Field:=2, Criteria1:="=S?", Operator:=xlAnd
    Case Is = 3
    Selection.AutoFilter Field:=2, Criteria1:="=G?", Operator:=xlAnd
    End Select
    Range("A1").Select
    SendKeys "{DOWN}", True
    r = ActiveCell.Row
    While Not ActiveCell.Value = ""
    SendKeys "{DOWN}", True
    Wend
    rr = ActiveCell.Row
    Range("A" & r & ":A" & rr).Copy
    Sheets("Teams").Select
    Select Case n
    Case Is = 1
    Range("A3").PasteSpecial
    Case Is = 2
    Range("D3").PasteSpecial
    Case Is = 3
    Range("G3").PasteSpecial
    End Select
    Next
    Sheets("SecResources").Select
    Selection.AutoFilter Field:=2
    Selection.AutoFilter Field:=3
    Sheets("Teams").Select
    Range("A13").Select

    End Sub
    [/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    MD
    thanks. I use your method all the time and actually have it several times in other places in the project. However, I startd this section late at night and have absolutely no idea why I used sendkeys. Normally don't for this type of movement.
    ttfn

    Kicker

  10. #10
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by lucas
    ...
    Range("A1").Select
    SendKeys "{DOWN}", True
    r = ActiveCell.Row
    While Not ActiveCell.Value = ""
    SendKeys "{DOWN}", True
    Wend
    ...
    [/vba]
    Aren't you just using that to replace Range("A2").End(xlDown).Select ?
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  11. #11
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Quote Originally Posted by johnske
    Aren't you just using that to replace Range("A2").End(xlDown).Select ?
    Glad I didn't have 2007 when I ran it....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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