Link columns from two different workbooks

2019-09-06 11:41发布

I'm trying to link data entered in Workbook 1 Sheet 2 Columns B:J to Workbook 2 sheet 1 Columns B:J. They're both on the same network and I have basic code to link the individual cells, but it's difficult to make changes to the Master log (workbook 1) without messing up data entered on workbook 2.

This is the code I'm trying to use but I get an error saying I don't have a source at Sub foo2() but I honestly have no idea what that means or even does. I just got this code from another post and am trying to get it to work. I changed the names here but I know what to enter for the path. I honestly don't even know if this is the right code for what I am trying to acheive.

Sub foo2()
Dim x As Workbook
Dim y As Workbook
On Error GoTo Errorcatch

'## Open both workbooks first:
Set x = Workbooks.Open(" S:\Blah\Blah FRC\Blah\Workbook 1 ")
Set y = Workbooks.Open(" S:\Blah\Blah FRC\Blah\Workbook 2 ")

'Now, transfer values from x to y:
y.Sheets("1").Range("B2:2000").Value = x.Sheets("1").Range("B2:B2000")

'Close x:
x.Close

End Sub

Exit Sub

Errorcatch:
 MsgBox Err.Description

1条回答
男人必须洒脱
2楼-- · 2019-09-06 12:19

Maybe try this:

Sub foo2()
  Dim x As Workbook
  Dim y As Workbook

  '## Open both workbooks first:
  Set x = Workbooks.Open(" S:\Blah\Blah FRC\Blah\Workbook 1 ")
  Set y = Workbooks.Open(" S:\Blah\Blah FRC\Blah\Workbook 2 ")

  'Now, transfer values from x to y:
  y.Sheets("1").Range("B2:2000").Value = x.Sheets("1").Range("B2:B2000")

  'Close x:
  x.Close

End Sub

Having an "Exit Sub" outside of the sub is never something I have seen, and I see how that could cause a debug error.

查看更多
登录 后发表回答