我在一个文件夹中有3个工作簿。
我使用宏将该文件夹中每个工作簿中的每个 Sheet1 复制到我的工作簿示例中。
在我的工作簿示例中,我有4张名为 sheet1 , sheet1(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
答案 0 :(得分:2)
问题是你没有正确引用对象
尝试使用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