如果B列不为空,则需要在L和M列中输入公式。
我的代码可以在我正在运行的活动工作表上工作,但不会填写其他工作表。我在一个工作簿中有23个工作表,它们具有相同的模板。
我是否以某种方式不在Cells(i, 12).FormulaR1C1 = "=RC[-1]-rc[-2]"
语句中包含R1C1行(With
)?
Sub forEachWs_Format_ForecastingTemplate()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Call Format_ForecastingTemplate_analysis_formulas(ws)
Next
End Sub
Sub Format_ForecastingTemplate_analysis_formulas(ws As Worksheet)
Dim cell As Range
Dim N As Long
Dim i As Long
N = Cells(Rows.Count, "B").End(xlUp).Row
For i = 7 To N
If ws.Cells(i, 2) <> "" Then
Cells(i, 12).FormulaR1C1 = "=RC[-1]-rc[-2]"
Cells(i, 13).FormulaR1C1 = "=RC[-2]-rc[-4]"
With Range(ws.Cells(i, 12), ws.Cells(i, 13))
.NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
End With
End If
Next
End Sub
答案 0 :(得分:0)
改为使用以下内容:
Sub forEachWs_Format_ForecastingTemplate()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Call Format_ForecastingTemplate_analysis_formulas(ws)
Next
End Sub
Sub Format_ForecastingTemplate_analysis_formulas(ws As Worksheet)
with ws
Dim cell As Range
Dim N As Long
Dim i As Long
N = .Cells(.Rows.Count, "B").End(xlUp).Row
For i = 7 To N
If .Cells(i, 2) <> "" Then
.Cells(i, 12).FormulaR1C1 = "=RC[-1]-rc[-2]"
.Cells(i, 13).FormulaR1C1 = "=RC[-2]-rc[-4]"
With Range(.Cells(i, 12), .Cells(i, 13))
.NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
End With
End If
Next
end with
End Sub
答案 1 :(得分:0)
@ cybernetic.nomad在评论中正确回答。