Populate userform with values from different worksheet

Mikeymike_W

Board Regular
Joined
Feb 25, 2016
Messages
171
Hi All,

I have a userform with several textboxes which I would like to populate using the values on another workbook.

The workbook I want to pull the values from is located here:

C:\Users\MWa\Desktop\VBA Test" & txtCDNumber.Value & ".xlsm"

I would like the above file to be opened in the background and closed once the values are extracted.

How would I reference the cell values in this workbook to populate the userforms different fields?

Many thanks for any help you can provide.

Mike
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Once you have the workbook open...
Code:
dim wbkname as string
wbkname = [COLOR=#333333]C:\Users\MWa\Desktop\VBA Test" & txtCDNumber.Value & ".xlsm"[/COLOR]
Set wbk = workbooks(wbkname)
'then to refer to specific sheet and cells....
userform1.textbox1 = wbk.sheets("whatever the sheet name is in quotes").range("A2")
 
Upvote 0
Hi Roderick,

Thanks for your help.

I am getting a "subscript out of range" error at

Code:
Set wbk = Workbooks(wbkname)
Code:

I've checked and double checked but the file name is correct. There is more than one sheet in the file I am pulling the data from, would I need to reference the particular sheet? If so would you be able to direct me?

Many thanks again,

Mike
 
Last edited:
Upvote 0
HI,

Managed to get it working, I used the code below

Code:
Dim wbkExternal                 As Workbook
Dim strExternalWBPathName       As String

strExternalWBPathName = "C:\Users\MWa\Desktop\VBA Test\" & txtCDNumber.Value & ".xlsm"
Set wbkExternal = Workbooks.Open(strExternalWBPathName)

Many thanks for your help,

Mike :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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