Solved

Buttons to import sheet and data from one workbook to another

Posted on 2014-09-21
6
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

695 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