Solved

Buttons to import sheet and data from one workbook to another

Posted on 2014-09-21
6
195 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Button function on table is in trouble 3 20
Excel 2016 - Edit Items in Custom Sort Level 5 19
Excel Calculation 4 48
Excel change font color in formula 19 28
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

808 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