我现在正在做的任务有问题。据说,我有一个for循环来计算使用范围(表)的 MIN 和 SUM 。由于我在工作簿中有10个工作表,我在工作簿中的每个工作表上添加了另一个for循环。但是,代码运行没有错误,但输出不是预期的。它甚至会在未使用的范围内计算。我的代码有错误吗?
Sub calc()
Dim ws As Worksheet
Dim y As Workbook
Dim rng As Range
Dim i As Integer, r As Long, j As Long
Set y = ThisWorkbook
For Each ws In y.Worksheets
For Each rng In ws.UsedRange.SpecialCells(xlCellTypeConstants, 3).Areas
If rng.Rows.Count > 1 And rng.Columns.Count = 14 Then
j = 2
r = rng.Cells(rng.Rows.Count, 1).Row + 1
Cells(r, rng.Columns(1).Column).Value = "SUMMARY"
For i = rng.Columns(2).Column To rng.Columns(2).Column + 12
If i = rng.Columns(12).Column Then
Cells(r, i).Formula = "=MIN(" & rng.Columns(j).Address & ")"
j = j + 1
Else
Cells(r, i).Formula = "=SUM(" & rng.Columns(j).Address & ")"
j = j + 1
End If
Next i
End If
Next rng
Next
End Sub