excel - 遍历工作簿中的所有工作表并添加公式
问题描述
您好我正在尝试将公式添加到工作簿中每个工作表的特定范围,但是当我运行时它无法正常工作。
公式是帮助计算满足特定条件的单元格数量的函数。
Sub Formatting()
For rcell = 1 To lrow
CharacterIndex = InStr(1, sourceSheet.Cells(rcell, Col_Western), "Delivery for Creative", vbBinaryCompare)
If CharacterIndex > 0 Then
On Error Resume Next
deliveryname = "CS"
With ThisWorkbook.Worksheets.add
.Name = deliveryname
sourceSheet.Range(sourceSheet.Cells(rcell, Col_Western), sourceSheet.Cells(lastrow, Col_phone).End(xlDown)).Copy .Range("A1")
Cells.Select
Selection.RowHeight = 50
Selection.ColumnWidth = 30
'Add Autofilter to Row above student details
Range("a8:e8").EntireRow.AutoFilter
End With
End If
Next rcell
For Each Grey_ws In ThisWorkbook.Worksheets
Call Grey_VALUE_AND_RANGE_ALL(Grey_ws)
'do nothing else
Next Grey_ws
End Sub
Sub Grey_VALUE_AND_RANGE_ALL(Grey_ws As Worksheet)
With Grey_ws
.Range("A5").FormulaR1C1 = "=Count_items_SmallWest()"
.Range("A6").FormulaR1C1 = "=Count_items_LargeWest()"
.Range("B5").FormulaR1C1 = "=Count_items_Small_Asian()"
.Range("B6").FormulaR1C1 = "=Count_items_Large_Asian()"
.Range("C5").FormulaR1C1 = "=Count_items_Small_Veg()"
.Range("C6").FormulaR1C1 = "=Count_items_Large_Veg()"
.Range("D5:D6").FormulaR1C1 = "=Count_items_Salad()"
.Range("E5:E6").FormulaR1C1 = "=Count_items_Dessert()"
.Range("F5:F6").FormulaR1C1 = "=Count_items_Snack()"
End With
End Sub
解决方案
我怀疑您的公式使用的用户定义函数在您的代码执行时被调用,从而扰乱了循环;Worksheet_Change 事件处理程序(如果存在)也可能会干扰您的循环。
在您的Formatting
潜艇中,将For
循环夹在和之间Application.Calculation
,Application.EnableEvents
如下所示:
Sub Formatting()
Dim Grey_ws as Worksheet
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
For Each Grey_ws In ThisWorkbook.Worksheets
Call Grey_VALUE_AND_RANGE_ALL(Grey_ws)
Next Grey_ws
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
这将使 Excel 知道在循环执行时它不应该计算任何公式结果,也不应该发出任何事件。
为了安全起见,请确保在出现错误时重新建立计算和事件:
Sub Formatting()
Dim Grey_ws As Worksheet
Dim lInitialCalculation As XlCalculation
Dim bInitialEnableEvents As Boolean
On Error GoTo ErrorHandler
lInitialCalculation = Application.Calculation
Application.Calculation = xlCalculationManual
bInitialEnableEvents = Application.EnableEvents
Application.EnableEvents = False
For Each Grey_ws In ThisWorkbook.Worksheets
Call Grey_VALUE_AND_RANGE_ALL(Grey_ws)
Next Grey_ws
Cleanup:
On Error Resume Next '<== important to prevent an infinite error loop, should the cleanup code fail.
If Application.EnableEvents <> bInitialEnableEvents Then
Application.EnableEvents = bInitialEnableEvents
End If
If Application.Calculation <> lInitialCalculation Then
Application.Calculation = lInitialCalculation '<== back to the initial value.
End If
Exit Sub
ErrorHandler:
MsgBox Err.Description, vbExclamation + vbOKOnly '<== or whatever you see fit.
GoTo Cleanup
End Sub
推荐阅读
- python - Dlib cuda 人脸检测 .dat 模型在 GTX 1650 上崩溃,而在许多其他 gpu 设备上表现良好
- python - 如何从缺少一些观察的索引中推断频率?
- android - 同时通过wifi(无互联网)和移动数据发送数据
- c# - 无法将值 NULL 插入到列“SchoolID”、表“TDC.dbo.SWprjectPart”中,这是从属下拉列表
- rest - 如何通过 gerrit rest api 获取访问具有特定组的 gerrit 项目?
- c# - 从 C# 中的 WSDL 生成的代码无法解析具有时区的日期
- javascript - Javascript/jQuery - 更改对象数据属性不会停止在 Chrome 中播放视频
- r - Shiny pickerInput - conditionalPanel 仅将值设置为第一个选择
- node.js - NestJS 基于接口注入自定义 TypeOrm 存储库
- git - 如何在某个提交时获得 git 子模块状态?