Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何循环工作簿中的每个工作表进行计算?_Vba_Excel - Fatal编程技术网

Vba 如何循环工作簿中的每个工作表进行计算?

Vba 如何循环工作簿中的每个工作表进行计算?,vba,excel,Vba,Excel,我现在正在做的任务有问题。据推测,我有一个for循环来计算所用范围(表)的最小值和总和。由于工作簿中有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 = ThisW

我现在正在做的任务有问题。据推测,我有一个for循环来计算所用范围(表)的最小值和总和。由于工作簿中有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

当您使用
单元格
范围
等时,您应该始终限定所指的工作表

比如说,

Cells(r, rng.Columns(1).Column).Value = "SUMMARY"
指的是活动的工作表,但您可能希望指的是
ws
所指的工作表,即

ws.Cells(r, rng.Columns(1).Column).Value = "SUMMARY"
您的代码可能如下所示:

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
        ws.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
                ws.Cells(r, i).Formula = "=MIN(" & rng.Columns(j).Address & ")"
                j = j + 1
            Else
                ws.Cells(r, i).Formula = "=SUM(" & rng.Columns(j).Address & ")"
                j = j + 1
            End If
        Next i
    End If
Next rng
Next

End Sub