VBA Excel 如何从 sheet 1 为 sheet 2、3 和 4 激活宏

VBA Excel how to activate macro for sheet 2, 3 and 4 from sheet 1

我在一个文件夹中有 3 个工作簿。
我使用宏将该文件夹中每个工作簿中的每个 Sheet1 复制到我的 工作簿示例中.
在我的 工作簿示例中 现在我有 4 个 sheet 名为 sheet1, sheet1(4),sheet1(3),sheet1(2).

我想使用按钮表单,所以当我单击它时,代码(下面)会运行除 sheet 之外的任何其他 sheet。

Sub Copy_Sum()
    Dim ws As Worksheet
    'Selecting the worksheets to loop through
    K = 1
    For Each ws In ThisWorkbook.Worksheets
    'Skiping the sheet1
        If ws.Name <> "Sheet1" Then
    'Counting the number of rows for automation
            rowscount = Cells(Rows.Count, 1).End(xlUp).Row
            temp = 0
    'add name
            Cells(rowscount + 1, 8) = "Jumlah"
            Cells(rowscount + 2, 8) = "Mutasi"
    'Looping throught the cells for the calculation
            For j = 2 To (rowscount)
               'Counting the number of cells which value greater than zero
                If Cells(j, 9) > 0 Then
                    temp = temp + 1
                End If
            Next j
    'Counting the number of rows for automation
            rowscount1 = Cells(Rows.Count, 1).End(xlUp).Row
            temp1 = 0
            For i = 2 To (rowscount1)
            'Counting the number of cells which value greater than zero
                If Cells(i, 10) > 0 Then
                    temp1 = temp1 + 1
                End If
            Next i
     'Summing up the values which are above the current cell
     'and in Sheet1, this inclues negative numbers as well
            Cells(rowscount + 1, 9).Value = Application.Sum(Range(Cells(1, 9), _
                Cells(rowscount, 9)))
            Cells(rowscount + 2, 9) = temp
            Cells(rowscount1 + 1, 10).Value = Application.Sum(Range(Cells(1, 10), _
                Cells(rowscount1, 10)))
            Cells(rowscount1 + 2, 10) = temp1
        End If
    Next ws
End Sub

我不是很懂宏代码。
这个代码是通过编辑NEOmen的代码制作的,非常感谢。
这段代码应该自动循环每个 sheet 的代码,但 sheet1 除外,但它不起作用。
我必须在 sheet1 (4)、sheet1 (3)、sheet1 (2) 中手动运行代码才能完成。
我想我可以像我想要的那样编​​辑它,但我不能。
最后卡住了

@chris neilsen @L42修改后的代码

Sub Copy_Sum()

Dim ws As Worksheet
'Selecting the worksheets to loop through
K = 1
For Each ws In ThisWorkbook.Worksheets
'Skiping the sheet1
With ws
    If .Name <> "Sheet1" Then
'Counting the number of rows for automation
         rowscount = .Cells(.Rows.Count, 1).End(xlUp).Row
         temp = 0
'add name
        .Cells(rowscount + 1, 8) = "Jumlah"
        .Cells(rowscount + 2, 8) = "Mutasi"
'Looping throught the cells for the calculation
             For j = 2 To (rowscount)
           'Counting the number of cells which value greater than zero
                  If .Cells(j, 9) > 0 Then
                  temp = temp + 1
                  End If
              Next j

'Counting the number of rows for automation
         rowscount1 = .Cells(.Rows.Count, 1).End(xlUp).Row
         temp1 = 0

              For i = 2 To (rowscount1)
           'Counting the number of cells which value greater than zero
                  If .Cells(i, 10) > 0 Then
                  temp1 = temp1 + 1
                  End If
              Next i

 'Summing up the values which are above the current cell and in Sheet1, this inclues negative numbers as well

             .Cells(rowscount + 1, 9).Value = Application.Sum(.Range(.Cells(1, 9), .Cells(rowscount, 9)))
             .Cells(rowscount + 2, 9) = temp

             .Cells(rowscount1 + 1, 10).Value = Application.Sum(.Range(.Cells(1, 10), .Cells(rowscount1, 10)))
             .Cells(rowscount1 + 2, 10) = temp1
             'copy ke sheet 1

             End If
    End With
Next ws

End Sub

问题是您没有正确引用对象。
尝试使用 With Statement.

完全限定您的对象
For Each ws In Thisworkbook.Worksheets
    With ws 'add With statement to explicitly reference ws object
        'precede all properties with a dot from here on
        If .Name <> "Sheet1" Then
            rowscount = .Cells(.Rows.Count, 1).End(xlUp).Row 'notice the dots
            temp = 0
            '~~> do the same with the rest of the code

        End If
    End With
Next