+ Reply to Thread
Results 1 to 6 of 6

Keep UserForm Linked to Particular Workbook Even if Focus is Lost

  1. #1
    Registered User
    Join Date
    01-19-2010
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2003
    Posts
    8

    Keep UserForm Linked to Particular Workbook Even if Focus is Lost

    I have an excel program that uses a modeless userform. often times i will open other spreadsheets while the form is active or minimized. if i return to the form and run it without manually bringing focus to the appropriate workbook, i get an error because it references the wrong workbook.

    is there a way to keep that form tied to that workbook no matter what? also, it is important that this procedure works despite the workbook name changing.

    thanks!
    Last edited by prattleon; 01-20-2010 at 01:49 PM.

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

    Re: Keep UserForm Linked to Particular Workbook Even if Focus is Lost

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!
    Hope that helps.

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

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    01-19-2010
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Keep UserForm Linked to Particular Workbook Even if Focus is Lost

    essentially I just want to know how to keep a userform focused on a workbook that might have a changing name. if there are multiple workbooks open, the userform must interact only with the workbook it was intended. if the name never changed it would be as simple as setting workbooks("filename.xls").active for each action on the userform, but "filename.xls" may change to anything. so i need to get the workbook name when the userform initializes:

    Dim wkb As Workbook
    Set wkb = ActiveWorkbook

    and then use:

    workbooks(wkb).active

    but the first part has to be done only in the initialization sub (so that it sets wkb to the correct workbook -- the one that initializes the form) and the other part in the other subs. but i am having trouble sending the variable wkb from one sub to another. is there an easier approach? more efficient?

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Keep UserForm Linked to Particular Workbook Even if Focus is Lost

    If the userform is in that workbook, then refer to ThisWorkbook, not to ActiveWorkbook. You can also use the Workbook's Activate and Deactivate events to show and hide the form as required.
    Remember what the dormouse said
    Feed your head

  5. #5
    Registered User
    Join Date
    01-19-2010
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Keep UserForm Linked to Particular Workbook Even if Focus is Lost

    Thank you. I used:

    With ThisWorkbook
    .Sheets("Sheet1").Range("A1:J10").Copy Destination:= _
    .Sheets("Sheet2").Range("B30")
    End With

    i did this everywhere and it appears to work. I thought ThisWorkbook referred to the workbook that had focus at the time that code was executed.

    Thanks again!

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Keep UserForm Linked to Particular Workbook Even if Focus is Lost

    It's the other way round:
    ThisWorkbook refers to the workbook containing the code; ActiveWorkbook is the one with the focus.

+ 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