Solved

Buttons to import sheet and data from one workbook to another

Posted on 2014-09-21
6
196 Views
Last Modified: 2014-09-26
I need a button (macro) to import/copy the contents of MasterWorkbookA , sheet2 to ClientWorkbookA, sheet2 (The button must be initiated by ClientWorkBookA). Eventually there will be 12 buttons on sheet1, each button will update a different sheet from master to client (appearing as a table of contents). The "MasterWorkbookA" is a spreadsheet with 12 different sheets (Each sheet with own button) updated by a supervisor, an admin should be able to open ClientWorkBookA which has nothing other than sheet one containing these buttons the first time it is opened. They look at the table of contents, choose a button, click on it, and it should then create the sheet with the identical name from MasterWorkbookA within ClientWorkBookA with the identical data. It is also important that any subsequent clicks on that particular button will fail because it has already been imported once. In other words, if the sheetname already exists in ClientWorkbookA than an import would fail. Thank you very much, If you can give me info for one button I can replicate for additional buttons. Thank you very much for your help!!!!
0
Comment
Question by:Mcottuli
  • 4
  • 2
6 Comments
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40336876
Try Something like this: (modified the code to suit ur needs, but code is not tested)

Dim directory As String, fileName As String, sheetName as String
'Turn off screen updating and displaying alerts.
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'initialize variables
directory = "c:\test\"
fileName = "MasterWorkbookA.xls"
'open the main workbook
Workbooks.Open (directory & fileName)
'get the sheet to copy depending on the button
sheetname="sheet2"
Workbooks(fileName).Worksheets(sheet.Name).Copy after:=Worksheets(Worksheets.count)
Workbooks(fileName).Close
'Turn back screen updating to on
Application.ScreenUpdating = True
Application.DisplayAlerts = True

Open in new window


Code Source: import sheets using excel

hope this helps
0
 
LVL 1

Author Comment

by:Mcottuli
ID: 40336967
Thank you for your help. I am running the button from ClientWorkbookA.xlsx. (Updated the  When I attempt to run it I get Run-Time error '424' Object Required. Hit Debug and get line 12 lighting up yellow. Any thoughts?
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40336980
instead of sheet.name, put sheetname
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Mcottuli
ID: 40337011
This time is does not error out, but the import does not happen. Attached are both files.
MasterWorkbookA.xlsx
ClientWorkbookA.xlsm
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40340931
i am currently overseas, and i only have my mac with me..
will try to check them for you when i get back home tomorrow!
regards,
0
 
LVL 11

Accepted Solution

by:
Dany Balian earned 500 total points
ID: 40345841
Try This: (it's tested and it works)

Sub Button1_Click()
Dim directory As String, fileName As String, sheetName As String
Dim w As New Workbook, wh As New Worksheet
'Turn off screen updating and displaying alerts.
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'initialize variables
directory = "c:\test\"
fileName = "MasterWorkbookA.xlsx"
'open the main workbook
Set w = Workbooks.Open(directory & fileName)
sheetName = "Test"
Set wh = w.Worksheets(sheetName)
'get the sheet to copy depending on the button
w.Worksheets(sheetName).Copy after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
w.Close
'Turn back screen updating to on
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub

Open in new window


the problem in the code was that, once you open a workbook..
the worksheets instance object becomes that open workbook; the solution was to simply replace the destination syntax of worksheets... with thisworkbook.worksheets...
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question