VBA Excel如何激活工作表1中的工作表2,3和4的macros

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

我想使用一个button表单,所以当我点击它时,代码(下面)运行除了表单之外的任何其他表单。

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 

我不完全理解macros代码。
这个代码是通过编辑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