繁体   English   中英

VBA Excel如何从工作表1激活工作表2、3和4的宏

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

我在一个文件夹中有3个工作簿。
我使用宏将该文件夹中每个工作簿中的每个Sheet1复制到我的工作簿示例中
现在在我的工作簿示例中,我有4张工作表,分别是sheet1sheet1(4), sheet1(3)和 sheet1(2)。

我想使用一个按钮表单,所以当我单击它时,代码(下面)将在除第一张纸之外的其他所有纸上运行。

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的代码制作的,我非常感谢。
该代码应该自动循环除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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM