Wednesday, January 31, 2007

How to auto link the two excel files using VB6.0

Private Sub AutoLinkWorkbookDemo(addressToCopy As String, destinationAddress As String)
Dim wb1 As Excel.Workbook
Dim wb2 As Excel.Workbook
Dim fileLocationWB1 As String
Dim fileLocationWB2 As String

fileLocationWB1 = ThisWorkbook.Path & "\wb1.xls"
fileLocationWB2 = ThisWorkbook.Path & "\wb2.xls"

'Opening both workbooks
Set wb1 = Workbooks.Open(fileLocationWB1)
Set wb2 = Workbooks.Open(fileLocationWB2)

'Copies required chunk of data to the clipboard
wb1.Worksheets(1).Range(addressToCopy).Copy

'Select the destination where the links will go
wb2.Worksheets(1).Range(destinationAddress).Select

'Paste the links to the required destination (i.e., at destinationAddress provided)
ActiveSheet.Paste link:=True

'Close source workbook without saving
wb1.Close savechanges:=False

'Close updated workbook with links to [wb1], saving the changes
wb2.Close savechanges:=True

'Destroy the objects
Set wb1 = Nothing
Set wb2 = Nothing
End Sub



Sub DemoTest()
AutoLinkWorkbookDemo addressToCopy:="A1:C10", destinationAddress:="D1"
End Sub

No comments: