VBA Excel如何从工作表1激活工作表2、3和4的宏
我在一个文件夹中有3本工作簿。VBA Excel如何从工作表1激活工作表2、3和4的宏,excel,foreach,worksheet,vba,Excel,Foreach,Worksheet,Vba,我在一个文件夹中有3本工作簿。我使用宏将该文件夹中每个工作簿中的每张表1复制到我的工作簿示例中 在我的工作簿示例中现在我有4张工作表,分别命名为sheet1,sheet1(4),sheet1(3),sheet1(2)。 我想使用一个按钮表单,因此当我单击它时,代码(下面)将运行于除第一张工作表之外的任何其他工作表 Sub Copy_Sum() Dim ws As Worksheet 'Selecting the worksheets to loop through K =
我使用宏将该文件夹中每个工作簿中的每张表1复制到我的工作簿示例中
在我的工作簿示例中现在我有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
子副本_Sum()
将ws设置为工作表
'选择要循环的工作表
K=1
对于此工作簿中的每个ws。工作表
“跳过床单1
如果ws.Name为“Sheet1”,则
'自动计算行数
rowscount=单元格(Rows.Count,1).End(xlUp).Row
温度=0
'添加名称
单元格(rowscount+1,8)=“Jumlah”
单元格(rowscount+2,8)=“Mutasi”
'循环通过单元格进行计算
对于j=2至(rowscount)
'计算值大于零的单元格数
如果单元格(j,9)>0,则
温度=温度+1
如果结束
下一个j
'自动计算行数
ROWSCONT1=单元格(Rows.Count,1).End(xlUp).Row
temp1=0
对于i=2至(第1行)
'计算值大于零的单元格数
如果单元(i,10)>0,则
temp1=temp1+1
如果结束
接下来我
'将当前单元格上方的值相加
在表1中,也包括负数
单元格(rowscount+1,9).Value=Application.Sum(范围(单元格(1,9))_
细胞(Rowsunt,9)))
单元格(rowscount+2,9)=温度
单元格(行单元格1+1,10)。值=应用程序。总和(范围(单元格(1,10))_
单元格(第1行、第10行)
单元格(行1+2,10)=temp1
如果结束
下一个ws
端接头
很抱歉,我不完全理解宏代码。这段代码是通过编辑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
子副本_Sum()
将ws设置为工作表
'选择要循环的工作表
K=1
对于此工作簿中的每个ws。工作表
“跳过床单1
与ws
如果。名称为“Sheet1”,则
'自动计算行数
rowscount=.Cells(.Rows.Count,1).End(xlUp).Row
温度=0
'添加名称
.单元格(rowscount+1,8)=“Jumlah”
.单元格(rowscount+2,8)=“Mutasi”
'循环通过单元格进行计算
对于j=2至(rowscount)
'计算值大于零的单元格数
如果.Cells(j,9)>0,则
温度=温度+1
如果结束
下一个j
'自动计算行数
RowScont1=.Cells(.Rows.Count,1).End(xlUp).Row
temp1=0
对于i=2至(第1行)
'计算值大于零的单元格数
如果.Cells(i,10)>0,则
temp1=temp1+1
如果结束
接下来我
'将当前单元格上方和表1中的值相加,这也包括负数
.Cells(rowsunt+1,9).Value=Application.Sum(.Range(.Cells(1,9),.Cells(rowsunt,9)))
.单元格(rowscount+2,9)=温度
.Cells(rowscount1+1,10).Value=Application.Sum(.Range(.Cells(1,10),.Cells(rowscount1,10)))
.单元格(行单元格1+2,10)=temp1
“复制第1页
如果结束
以
下一个ws
端接头
问题在于您没有正确引用对象。尝试使用
With语句对对象进行完全限定
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
此工作簿中每个ws的。工作表
使用ws的add With语句显式引用ws对象
'从这里开始,在所有属性前面加一个点
如果。名称为“Sheet1”,则
rowscount=.Cells(.Rows.Count,1).End(xlUp).Row'注意点
温度=0
“~~>对其余的代码执行相同的操作
如果结束
以
下一个
所有的单元格
和范围
引用都指向活动表。也许它们应该是ws.Cells
和ws.Range
@chrisneilsen-ah在发布我的答案时没有看到你的评论。如果你想添加它作为答案,我将删除我的:)@l42没问题,只需将其保留为Iswooo。。非常感谢@chrisneilsen它真的解决了我的问题。我花了大约一个星期的时间试图解决这个问题,但最终还是在你的帮助下完成了。再次感谢:D如果你不介意的话,我想问一下。为什么我需要在每个单元格和范围中添加点以使其正确?即使是现在我也无法理解代码是否正确。啊,很抱歉我的英语不好。。非常感谢@L42它真的解决了我的问题。我花了大约一个星期的时间试图解决这个问题,但最终还是在你的帮助下完成了。再次感谢:D如果你不介意的话,我想问一下。为什么我需要在每个单元格和范围中添加点以使其正确?即使是现在我也无法理解代码是否正确。啊,对不起,我的英语不好。因为一次只能提到一个用户,所以我写了另一条评论。