Solved

Buttons to import sheet and data from one workbook to another

Posted on 2014-09-21
6
190 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now