Open 1 WorkBook with 2 WorkSheets from TextBoxes

Andresleo47

Board Regular
Joined
Oct 29, 2008
Messages
132
Hi Community! I am trying to open one Workbook with the 2 TimeSheets in the attached TextBoxes.

This is the code I have for now, but it's giving me an error:

Sub CommandButton1_Click()

Workbooks.Add.SaveAs Filename:="ThisWeek.xlsx"
ActiveWorkbook.Worksheets.Add Count:=1

With Sheets("Sheet1")
Workbooks.Open .TextBox1.Text
End With
With Sheets("Sheet2")
Workbooks.Open .TextBox2.Text
End With
End Sub


Any ideas would be appreciated!!
 

Attachments

  • Capture5.PNG
    Capture5.PNG
    22.4 KB · Views: 10
Thanks Johnny. With this newly created Active Workbook, I would like to get the data stored in both paths from the TextBoxes in 2 different sheets. Not sure if I'm making sense. Each TextBox leads to different WorkBooks (reports that I download), and I want to build a new file called "ThisWeek" where I can put the content of one TextBox in one Sheet and the content of the second Textbox in another sheet.

Let me know if you have questions, because I'm not even sure if it's clear :(

Thanks,

Regards,
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
If my math is correct, you want to use one workbook to create a new workbook, then open two more workbooks and copy a sheet from each one of those two workbooks, into the new workbook.
 
Upvote 0
The first Workbook is the Macro, standard. I put it in a different file because if I need to send it over to other people, it would work. Screenshot attached and another post will follow after this one...
 

Attachments

  • Capture3.PNG
    Capture3.PNG
    18.1 KB · Views: 2
Upvote 0
When I hit the CommandButton called "Open Working File", a new Excel File (Workbook) must be opened with 2 Worksheets (Those new Worksheets would be the files that are in the TextBoxes).
 

Attachments

  • Capture10.PNG
    Capture10.PNG
    157.7 KB · Views: 2
  • Capture11.PNG
    Capture11.PNG
    134.8 KB · Views: 2
Upvote 0
After that, I need to do some manipulation, Pivot Tables and stuff, but I think that's easier. Hope that makes sense?

Let me know if you have any questions.

Andres
 
Upvote 0
In other words, how do I open a WorkBook from a path that is clustered in a TextBox? :)

Example: If Sheet1 is the sheet name that contains the textbox, and the textbox that you are interested in is TextBox1

VBA Code:
Application.Workbooks.Open Sheets("Sheet1").TextBox1.Value
 
Upvote 0
Hi ,thanks for replying.

So, TextBox1 and TextBox2 have 2 different paths, both of them lead to different Excel files. When I download those 2 reports from the server, they come already with names in the Sheets. The name of the sheet is "report" for both files. However, I don't want to affect any of those 2 files, that's why I'm trying to copy-paste them in a new file: "ThisWeek".

I wish I could send a Doc file instead of an image.

Thanks so much!

Regards,
 
Upvote 0
I think the following will do what I think you are wanting:

VBA Code:
Sub CommandButton1_Click()
'
    Dim ClosedSourceWorkbook    As Workbook
'
    Workbooks.Add.SaveAs Filename:="ThisWeek.xlsx"                                          ' Create/Name/save New Workbook
'
    ThisWorkbook.Activate                                                                   ' Reactivate this workbook
'
    Application.ScreenUpdating = False                                                      ' Turn ScreenUpdating off
'
    Set ClosedSourceWorkbook = Workbooks.Open(Sheets("Sheet1").TextBox2.Value)              ' Get value from TextBox to open the source file
    ClosedSourceWorkbook.Sheets("report").Copy Before:=Workbooks("ThisWeek.xlsx").Sheets(1) ' Copy sheet to beginning of the newly created workbook
    ClosedSourceWorkbook.Close SaveChanges:=False                                           ' Close the source workbook
'
    ThisWorkbook.Activate                                                                   ' Reactivate this workbook
'
    Set ClosedSourceWorkbook = Workbooks.Open(Sheets("Sheet1").TextBox1.Value)              ' Get value from TextBox to open the source file
    ClosedSourceWorkbook.Sheets("report").Copy Before:=Workbooks("ThisWeek.xlsx").Sheets(1) ' Copy sheet to beginning of the newly created workbook
    ClosedSourceWorkbook.Close SaveChanges:=False                                           ' Close the source workbook
'
    Application.DisplayAlerts = False                                                       ' Turn off DisplayAlerts to avoid sheet deletion warning
    Sheets("Sheet1").Delete                                                                 ' Delete Sheet1 from the newly created workbook
    Application.DisplayAlerts = True                                                        ' Turn DisplayAlerts back on
'
    ActiveWorkbook.Close True                                                               ' Save newly created file and close
'
    Application.ScreenUpdating = True                                                       ' Turn ScreenUpdating back on
End Sub
 
Upvote 0
I think the following will do what I think you are wanting:

VBA Code:
Sub CommandButton1_Click()
'
    Dim ClosedSourceWorkbook    As Workbook
'
    Workbooks.Add.SaveAs Filename:="ThisWeek.xlsx"                                          ' Create/Name/save New Workbook
'
    ThisWorkbook.Activate                                                                   ' Reactivate this workbook
'
    Application.ScreenUpdating = False                                                      ' Turn ScreenUpdating off
'
    Set ClosedSourceWorkbook = Workbooks.Open(Sheets("Sheet1").TextBox2.Value)              ' Get value from TextBox to open the source file
    ClosedSourceWorkbook.Sheets("report").Copy Before:=Workbooks("ThisWeek.xlsx").Sheets(1) ' Copy sheet to beginning of the newly created workbook
    ClosedSourceWorkbook.Close SaveChanges:=False                                           ' Close the source workbook
'
    ThisWorkbook.Activate                                                                   ' Reactivate this workbook
'
    Set ClosedSourceWorkbook = Workbooks.Open(Sheets("Sheet1").TextBox1.Value)              ' Get value from TextBox to open the source file
    ClosedSourceWorkbook.Sheets("report").Copy Before:=Workbooks("ThisWeek.xlsx").Sheets(1) ' Copy sheet to beginning of the newly created workbook
    ClosedSourceWorkbook.Close SaveChanges:=False                                           ' Close the source workbook
'
    Application.DisplayAlerts = False                                                       ' Turn off DisplayAlerts to avoid sheet deletion warning
    Sheets("Sheet1").Delete                                                                 ' Delete Sheet1 from the newly created workbook
    Application.DisplayAlerts = True                                                        ' Turn DisplayAlerts back on
'
    ActiveWorkbook.Close True                                                               ' Save newly created file and close
'
    Application.ScreenUpdating = True                                                       ' Turn ScreenUpdating back on
End Sub

Hi, thanks so much!!

I tried it, but this comes up (See attached).

Any thoughts? Maybe it's a server issue?

Not sure! Thanks for your efforts and help!
 

Attachments

  • Capture12.PNG
    Capture12.PNG
    92.9 KB · Views: 5
  • Capture13.PNG
    Capture13.PNG
    72.4 KB · Views: 6
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,973
Members
448,933
Latest member
Bluedbw

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