Copy userform data to another opened workbook

YingFa

Board Regular
Joined
Nov 4, 2019
Messages
63
Hello,

I have the following code to open a workbook called file1.xlsm from a userform. I would like to copy textbox1 to cell A1 in the opened file1 workbook but the code that I have is not working for that. It only opens the workbook but it does not copy textbox1 value into cell A1. Can I please have your help on this?




Code:
Private Sub CommandButton1_Click()

Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
Application.ScreenUpdating = False
xlApp.Visible = True
xlApp.Workbooks.Open Filename:="Z:\QZX\Project\File1.xlsm"

Dim Workbook As Workbook
Dim sheet As Worksheet
Set Workbook = Workbooks("File1")
Set sheet = Worksheets("File1")
sheet.Cells(1, 1) = Me.TextBox1.Value

Application.ScreenUpdating = True

End Sub

Thank you.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I believe you must change the set worksheets line of code from file1 to the name or number of the sheet you want to copy to.
 
Upvote 0
The name of the sheet is the same as the name of the workbook. I still cannot make it work. Do you have any idea what else it can be besides changing the set worksheets?
 
Upvote 0
First I wouldn't use "workbook" as a variable name. Its too close to a reserved word for my liking. Or Sheet for that matter

But to your point, you need to specify which workbook the worksheet is from

Code:
Dim wb As Workbook, sh as Worksheet

Set wb = Workbooks("File1")
Set  sh = wb.Worksheets("File1")

sh.Cells(1, 1) = Me.TextBox1.Value

Also, is the name of the workbook File1 or File1.xlsm
 
Upvote 0
How about
VBA Code:
Private Sub CommandButton1_Click()
    Dim Wbk As Workbook
    Dim Ws As Worksheet
    
    Application.ScreenUpdating = False
    Set Wbk = Workbooks.Open(Filename:="Z:\QZX\Project\File1.xlsm")
    Set Ws = Wbk.Worksheets("File1")
    Ws.Cells(1, 1) = Me.TextBox1.Value
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Hello #mikerickson. Thank you for your contribution on this. I have tried changing the code for the one you suggested and it gave me error 91: object variable or with block variable not set. Then, when I debug the highlighted line showing the error is set sh = wb.Worksheets(“File1”) and the value in text box one is not being copied to the workbook, I don’t understand why is giving me that error
 
Upvote 0
You have that problem because you are opening the file in a new instance of Xl.
 
Upvote 0
How about
VBA Code:
Private Sub CommandButton1_Click()
    Dim Wbk As Workbook
    Dim Ws As Worksheet
    
    Application.ScreenUpdating = False
    Set Wbk = Workbooks.Open(Filename:="Z:\QZX\Project\File1.xlsm")
    Set Ws = Wbk.Worksheets("File1")
    Ws.Cells(1, 1) = Me.TextBox1.Value
    
    Application.ScreenUpdating = True

End Sub

Thank you very much Fluff! The code you provided is working perfectly. I can’t say thank you enough for your help!
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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