首页 > 解决方案 > 设置范围不适用于内部的 Cells()

问题描述

我正在编写一个代码,在其中设置这两个范围:

Set ColorSAP = SAP_Comparison.Range("C3:AG19")
Set ColorPlan = Production_Plan.Worksheets("Schedule").Range(Cells(4, 
MonthS), Cells(20, (MonthE - 1)))

MonthS并且MonthE是整数。

Production_plan是一本工作簿。

SAP_Comparison是一本工作簿。

我不知道为什么第二个错误,但第一个错误,因为它们非常相似。我什至测试过更简单的案例,这种用法确实有效。

Error: Run-Time '1004'
Application-defined or object-defined error.

我该如何解决这个问题?

标签: excelvba

解决方案


Unqualified Cells 是指 ActiveSheet,如果不是“Schedule”,它将引发错误

你需要类似的东西

With Production_Plan.Worksheets("Schedule")
    Set ColorPlan = .Range(.Cells(4, MonthS), .Cells(20, (MonthE - 1)))
End With

或者

Dim sht As Worksheet

Set sht = Production_Plan.Worksheets("Schedule")
Set ColorPlan = sht.Range(sht.Cells(4, MonthS), sht.Cells(20, (MonthE - 1)))

工作表、单元格和范围的默认范围是什么?


推荐阅读