excel - 我似乎无法指定执行宏的工作表。无论我做什么,我都会收到错误 9
问题描述
我有两张床单。“销售和营销”。我想在“销售”表上制定宏。我在第 4 行不断收到错误 9。
Sub halveandcolorchange()
Dim Sa As Worksheet
Dim cell As Range, rng As Range, A As Range, LastRow As Long
Set Sa = ThisWorkbook.Worksheets("Sales")
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
Set rng = Sa.Range("B1:B" & LastRow)
For Each cell In rng
Set A = cell.Offset(0, -1)
If cell.Value > 3 Then
A.Value = A.Value / 2
A.Interior.ColorIndex = 3
End If
Next cell
End Sub
解决方案
试试这个代码:
Sub halveandcolorchange()
Dim Sa As Worksheet
Dim cell As Range, rng As Range, A As Range, LastRow As Long
Dim B As Double
Set Sa = ThisWorkbook.Worksheets("Sales")
LastRow = Sa.Cells(Sa.Rows.Count, "B").End(xlUp).Row
Set rng = Sa.Range("B1:B" & LastRow)
For Each cell In rng
Set A = cell.Offset(0, -1)
If cell.Value > 3 Then
B = cell.Value
A.Value = B / 2
A.Interior.ColorIndex = 3
End If
Next cell
End Sub
推荐阅读
- c++ - 为什么 vtkClipPolyData 在 vtk 的表面渲染输出中渲染速度非常慢
- java - Maven Spring Boot 编译失败
- r - 如何找到一个值在 R 数据集中的不同列中出现的实例数?
- delphi - 如何在 fmx delphi 中为 utf8 语言(如波斯语)模拟键盘事件?
- javascript - 通过 html datalist 和 *ngFor 显示一个值并提交另一个值
- google-apps-script - 如何将 onEdit 限制为特定单元格中的更改?
- android - 如何使用 FilePcker.Builder 的相机选项隐藏我正在拍摄的画廊中的图像?
- kubernetes - glusterfs heketi 使用分区而不是原始设备以允许轻松扩展
- php - 发送多封电子邮件时出错,应该只发送一封
- rest - 如果休息端点正在使用外部休息端点,Swagger/其他 API 文档/工具能否提供信息