excel - 设置范围不适用于内部的 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.
我该如何解决这个问题?
解决方案
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)))
推荐阅读
- ruby-on-rails - Rails在孩子上将foreign_key设置为nil的首选方式
- c++ - 如何将 C++ 类的静态成员函数传递给模板并调用该函数
- python - 如何删除列表中的dict(JSON对象)?
- sql - 从当前月份获取当前天数,节假日除外
- uwp - 移除 NavigationMenuItem 上的悬停动画
- python - 检测到物体后如何知道物体是否在运动?
- javascript - 焦点未正确显示图形 D3 javascript
- java - 如何从使用 selenium webdriver 中的 api 响应动态启动的下拉列表中选择特定位置?
- javascript - Angular 6 @Viewchild 不适用于延迟加载
- javascript - react-router中如何更新history.replace上的路由器状态?