0
我遇到了我現在正在做的任務的問題。據推測,我有一個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
嗨YowE3K,謝謝!它工作得很好。 – mmaz