Referencing Query in VBA

mExd2014

New Member
Joined
Dec 30, 2014
Messages
39
Hi all

Scenario as follows;

I have a userform that is set up which will be run from any workbook (the workbook will always have a unique name). However the userform is set up to open another workbook in the background when a command button is clicked.

My problem is that I want to extract some data from the unique workbook when the command button is initialised.

Where can I put the code so that the original workbook becomes the active workbook, bearing in mind that when the userform is selected, the workbook in the background becomes the active workbook.

Hope this makes some sense.

Thanks in advance
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
You can place some code such as this at the beginning of your macro. It defines the variable "origWB" to refer to the original workbook which contains the macro. Then you can use that variable to refer to the original workbook and perform any action on it.
VBA Code:
Dim origWB As Workbook
Set origWB = ThisWorkbook
 
Upvote 0
So the macro is just stored in the personal workbook rather than a particular workbook so the userform can be initialised on any workbook.

If I include it in the command button part of the code, it will refer to this workbook as the one that opens in the background?
 
Upvote 0
This c ode assumes that you have only the two workbooks open:
VBA Code:
Dim origWB As Workbook, wb As Workbook
For Each wb In Workbooks
    If wb.Name <> ActiveWorkbook.Name Then
        Set origWB = Workbooks(wb.Name)
    End If
Next wb
 
Upvote 0
You are very welcome. :)
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top