VBA从一个工作簿复制到另一个工作簿

我正在尝试将信息从一个工作簿复制到另一个现有工作簿,但正在添加错误的信息。

Sub Set_Open_ExistingWorkbook() Dim UserRoleWkb As Workbook, ConfigWkb As Workbook, UserRoleWkst As Worksheet, ConfigWkst As Worksheet Set UserRoleWkb = Workbooks.Open("C:\Users\cs\Desktop\Ar.xlsx") **Set ConfigWkb = ActiveWorkbook** Set UserRoleWkst = UserRoleWkb.Sheets("RS Users") **Set ConfigWkst = ActiveWorkbook.ActiveSheet** Dim i As Integer, j As Integer j = 10 'user role For i = 8 To 16 'config If ConfigWkst.Cells(i, 2).Value <> "" Then UserRoleWkst.Cells(j, 2).Value = ConfigWkst.Cells(i, 2).Value j = j + 1 End If Next i End Sub 

**的代码部分是围绕它的问题。 当我调用ActiveWorkbook和ActiveSheeet时,它从UserRoleWkst(工作表)中获取信息,而不是我想要的:Configwkst。

我已经尝试过,但会在ConfigWkst行上得到错误,代码将不会运行:

  Sub Set_Open_ExistingWorkbook() Dim UserRoleWkb As Workbook, ConfigWkb As Workbook, UserRoleWkst As Worksheet, ConfigWkst As Worksheet Set UserRoleWkb = Workbooks.Open("C:\Users\cs\Desktop\Ar.xlsx") **Set ConfigWkb = ActiveWorkbook** Set UserRoleWkst = UserRoleWkb.Sheets("RS Users") **Set ConfigWkst = ConfigWkb.Sheets("Users") Dim i As Integer, j As Integer j = 10 'user role For i = 8 To 16 'config If ConfigWkst.Cells(i, 2).Value <> "" Then UserRoleWkst.Cells(j, 2).Value = ConfigWkst.Cells(i, 2).Value j = j + 1 End If Next i End Sub 

任何帮助表示赞赏。 谢谢!

一旦你打开另一个工作簿,那么这将成为ActiveWorkbook,所以你需要打开UserRoleWkb之前获得该参考

 Set ConfigWkb = ActiveWorkbook Set ConfigWkst = ConfigWkb.ActiveSheet Set UserRoleWkb = Workbooks.Open("C:\Users\cs\Desktop\Ar.xlsx") Set UserRoleWkst = UserRoleWkb.Sheets("RS Users")