Copy data from another workbook on Open and hide all sheets except 1 at the end.

BODYCOTE

New Member
Joined
May 18, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello I want to do the following

When opening Workbook (a)>
open workbook (b)>
copy 'table1' from workbook (b)
paste 'table1' into workbook (a) table '1'
close workbook (b)
hide all worksheets on workbook (a) except 'HOME' worksheet

i have the following code returning runtime error '1004';
Application-defined or object-defined error

VBA Code:
Private Sub Workbook_Open()
'
' OpenWorkbook Macro

    Application.ScreenUpdating = False
    Sheets("STOCKLIST").Select
    ActiveSheet.Unprotect Password:="lavish123"

  Workbooks.Open "C:\Users\posuser\Dropbox\Synagogue Management\Bars & License\Cost Sheets\0. Master- Cost Sheet.xlsm"
  
  Workbooks("0. Master- Cost Sheet.xlsm").Worksheets("STOCKLIST").Range("Table1").Copy
  
  ThisWorkbook.Worksheets("STOCKLIST").Range("Table1").PasteSpecial Paste:=xlPasteAll
  
  Workbooks("0. Master- Cost Sheet.xlsm").Worksheets("SUPPLIERS").Range("Table6").Copy

  ThisWorkbook.Worksheets("SUPPLIERS").Range("Table6").PasteSpecial Paste:=xlPasteAll
  
  Workbooks("0. Master- Cost Sheet.xlsm").Close SaveChanges:=False
  
  Worksheets("HOME").Activate

Dim sh As Worksheet
For Each sh In Worksheets
If sh.Name <> "HOME" Then sh.Visible = xlSheetHidden
Next
End Sub

Thanks for any help
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Code:
Public Sub HideAll()
Dim ws As Worksheet

On Error Resume Next
Sheets("Home").Activate
For Each ws In Sheets
  If ws.Name = "HOME" Then
     ws.Visible = xlSheetVisible
  Else
     ws.Visible = xlSheetHidden
  End If
Next
Set ws = Nothing
End Sub
 
Upvote 0
Thanks for the reply, how do i incorporate the rest of the vba to open and copy for the other workbook
 
Upvote 0
Hi,
not tested but see if this update to your code does what you want


VBA Code:
Private Sub Workbook_Open()
    Dim wbA            As Workbook
    Dim sh             As Worksheet, wsHome As Worksheet
    Dim wsStockList    As Worksheet, wsSuppliers As Worksheet
    Dim FolderPath     As String, FileName As String
    '
    ' OpenWorkbook Macro
    
    FolderPath = "C:\Users\posuser\Dropbox\Synagogue Management\Bars & License\Cost Sheets\"
    FileName = "0. Master- Cost Sheet.xlsm"
    
    On Error GoTo myerror
    
    Set wsStockList = ThisWorkbook.Worksheets("STOCKLIST")
    Set wsSuppliers = ThisWorkbook.Worksheets("SUPPLIERS")
    Set wsHome = ThisWorkbook.Worksheets("HOME")
    
    wsHome.Visible = xlSheetVisible
    
    wsStockList.Unprotect Password:="lavish123"
    
    Application.ScreenUpdating = False
    
    
    Set wbA = Workbooks.Open(FolderPath & FileName, False, True)
    
    wbA.Worksheets("STOCKLIST").Range("Table1").Copy wsStockList.Range("Table1").Cells(1, 1)
    
    wbA.Worksheets("SUPPLIERS").Range("Table6").Copy wsSuppliers.Range("Table6").Cells(1, 1)
    
    For Each sh In ThisWorkbook.Worksheets
        sh.Visible = sh.Name = wsHome.Name
    Next
    
myerror:
    If Not wbA Is Nothing Then wbA.Close False
    wsStockList.Protect Password:="lavish123"
    wsHome.Activate
     
    Application.ScreenUpdating = True
        
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
         
     
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,331
Members
449,077
Latest member
jmsotelo

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