繁体   English   中英

VBA Excel 2007:需要循环复制和循环计数,但上面每行除外零

[英]VBA Excel 2007 : Need to loop copy and loop count number except zero every row above

我是vba中的完全菜鸟,所以我正在网上搜索所有代码以合并代码,但是现在看来我碰到了长城,无法正确解决。 我想做的是:

  • 总结上面的每一行并在上面添加额外的行(以某种方式我正确了)
  • 在额外的行中(我在上面说过),我想对值大于零的每个上面的单元格进行计数(在Excel中,如果使用公式,我会使用简单的计数,但是我无法在vba中执行此操作)
  • 在工作表中除工作表1之外的其他工作表中循环上述步骤(工作表的数量可以根据输入而变化,所以我相信可以循环执行,但我不知道如何操作)
  • 将上述步骤的输出复制到工作表1中

到目前为止,这是我的代码,由于我无法执行循环,因此我手动完成了sheet2和sheet3的操作。 我陷入了步骤2

这是从@NEOman'代码中获取的经过修改的代码

    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
'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
             'copy ke sheet 1
             Worksheets("Sheet1").Cells(K, 1).Value = Cells(rowscount + 1, 1).Value
             Worksheets("Sheet1").Cells(K, 2).Value = temp
             K = K + 1

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

             End If

Next ws

End Sub

我知道我的代码很乱,我在每个步骤中都写了注释,以便我知道代码在做什么。 我为I和J列使用了不同的代码,但均无效:(。任何帮助将不胜感激,在此先感谢您的关注。

================================================== ========================================

该代码必须在每个工作表(除sheet1之外)中手动运行,因此我仍然试图使该代码从sheet1运行,但可以在同一工作簿中的任何其他工作表上运行。 任何帮助将不胜感激,在此先感谢您的关注。

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
'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 - 1, 1) > 0 Then
                  temp = temp + 1
                  End If
              Next j
 'Summing up the values which are above the current cell and in Sheet1, this inclues negative numbers as well

             Cells(rowsCount + 1, 1).Value = Application.Sum(Range(Cells(1, 1), Cells(rowsCount, 1)))
             Cells(rowsCount + 1, 2) = temp
             Worksheets("Sheet1").Cells(K, 1).Value = Cells(rowsCount + 1, 1).Value
             Worksheets("Sheet1").Cells(K, 2).Value = temp
             K = K + 1
             End If

Next ws

End Sub

暂无
暂无

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

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