Workbook.ActiveSheet中的运行时错误9(下标超出范围)

时间:2021-07-04 16:40:19

I'm using a userform to copy values from one workbook to another.

我正在使用userform将值从一个工作簿复制到另一个工作簿。

Workbook.ActiveSheet中的运行时错误9(下标超出范围)

The copying part of my VBA is this:

我的VBA的复制部分是这样的:

If Form.ComboBox2.Value <> "" Then
    Set sourceColumn = wb.Worksheets(cmb).Columns("B")
    Set targetColumn = Workbooks("B").ActiveSheet.Columns("A")
    sourceColumn.Copy Destination:=targetColumn

I'm getting a runtime error 9: Subscript out of range at Set targetColumn = Workbooks("B").ActiveSheet.Columns("A").

我收到运行时错误9:下标超出范围,设置targetColumn =工作簿(“B”)。ActiveSheet.Columns(“A”)。

Workbook.ActiveSheet中的运行时错误9(下标超出范围)

I'm a noob at VBA and reading up made me realize that this means that targetColumn isnt getting a value. I wonder why though. Whats going wrong with this?

我是VBA的菜鸟,阅读让我意识到这意味着targetColumn没有获得价值。我想知道为什么。这有什么问题吗?

1 个解决方案

#1


0  

debugging tip:

调试提示:

put in these lines before Set targetColumn = Workbooks("B").ActiveSheet.Columns("A")

在设置targetColumn = Workbooks(“B”)之前放入这些行.ActiveSheet.Columns(“A”)

dim aaa as object
Set aaa = Workbooks("B")
Set aaa = Workbooks("B").ActiveSheet

and run your code again

并再次运行您的代码

which one fails now?

哪一个现在失败了?

#1


0  

debugging tip:

调试提示:

put in these lines before Set targetColumn = Workbooks("B").ActiveSheet.Columns("A")

在设置targetColumn = Workbooks(“B”)之前放入这些行.ActiveSheet.Columns(“A”)

dim aaa as object
Set aaa = Workbooks("B")
Set aaa = Workbooks("B").ActiveSheet

and run your code again

并再次运行您的代码

which one fails now?

哪一个现在失败了?