Linking Workbook to a different workbook using VBA

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
834
Office Version
  1. 365
Platform
  1. Windows
I have searched for this and have not come up with anything.
I have 2 workbooks, 1 named "Main Workbook" and another called "Public Workbook"
is there code that can be but into sheet 1 of the "Public Workbook" that will do the same as a formula such as this

='[main book.xls]Sheet1'!$A:$N
 
If you use the above formula in cell A1 of the Public workbook, then select columns A:N and press Ctrl-Shift-Enter the formula will be replicated over the entire range as an array that will contain the adjusted formula.

This will probably make your workbook nearly unuseable due to the recalc time required whenever a cell is changed.

The following code will copy the columns from "Main Workbook" whenever it is executed.

Code:
Option Explicit

Sub CopyFromMain()

    Dim secAutomation As MsoAutomationSecurity
    secAutomation = Application.AutomationSecurity

    Application.AutomationSecurity = msoAutomationSecurityForceDisable 'disables macros in workbook being opened
    
    Dim sDirPath As String
    Dim wbk As Workbook
    
    sDirPath = "F:\Test\" 'your path here, must end in \
    ThisWorkbook.Worksheets("Sheet1").Range("A:N").Cells.Clear
    
    Workbooks.Open sDirPath & "Main Workbook.xls", ReadOnly:=True
    Set wbk = ActiveWorkbook
    
    With wbk.Worksheets("Sheet1")
        .Range("A:N").Copy Destination:=ThisWorkbook.Worksheets("Sheet1").Range("A1")
    End With
    
    wbk.Close
    
    Set wbk = Nothing
    Application.AutomationSecurity = secAutomation
    
End Sub
 
Upvote 0

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