Need to switch focus back to workbook from VBA form

I have an Excel userform with a Part Number textbox that users enter data to, (a SQL db search uses this data to return a query) .
The form works just fine when users do not switch from the workbook that contains the form (MORF.xls) to another workbook.
Problem happens when they open the form, then switch to another workbook (to copy/paste their Part Number), then paste the data to the form. The Activeworkbook is no longer MORF.xls, thus all my workbook references (eg; Set SelectedSites_AllChk = Range("E7")) refer to the other workbook which is active.

What I need is a method to publicly declare the name of the MORF.XLS workbook, and make this available to the code in the userform.

Note that I have a Sub in a Module (Sub ShowForm()) which shows the form, then a button on the form which runs the SQL query.
I believe I need to be able to assign the workbook name in the ShowForm Sub, then pass this to a workbook variable in the userform.
Thanks a lot,
Fergal
fjkilkenAsked:
Who is Participating?
 
Rgonzo1971Commented:
Hi,

then use

Set SelectedSites_AllChk = ThisWorkbook.ActiveSheet.Range("E7")

'ThisWorkbook' refers to the Wb where the macro is running

Regards
0
 
fjkilkenAuthor Commented:
Wow! that was fast....
Such a simple but 100% effective fix - thanks a lot Rgonzo1971!!!
0
 
fjkilkenAuthor Commented:
perfect - thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.