我正在尝试将信息从一个工作簿复制到另一个现有工作簿,但是添加了错误的信息。
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
感谢任何帮助。谢谢!
答案 0 :(得分:1)
一旦打开另一个工作簿,那么它将成为ActiveWorkbook,因此您需要在打开UserRoleWkb之前获取该参考
int randomIndex = generate.nextInt(names.size());
Answer.setText(randomIndex + " moet koffie halen!");