简体   繁体   English

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

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

i'm a complete noob in vba so i'm searching all over the net to combine the code but right now it seems i hit the great wall and can't get it right. 我是vba中的完全菜鸟,所以我正在网上搜索所有代码以合并代码,但是现在看来我碰到了长城,无法正确解决。 what i wanna do are: 我想做的是:

  • to sum every row above and add extra row above (somehow i get this right) 总结上面的每一行并在上面添加额外的行(以某种方式我正确了)
  • in extra row (i said above) i want to count every cells above that have value more than zero (in excel i use simple count if formula but i cant do it in vba) 在额外的行中(我在上面说过),我想对值大于零的每个上面的单元格进行计数(在Excel中,如果使用公式,我会使用简单的计数,但是我无法在vba中执行此操作)
  • to loop the step above in another sheet in this workbook except sheet 1 (the quantity of sheets can vary depend on the input, so i believe this can be done by loop but i dont know how) 在工作表中除工作表1之外的其他工作表中循环上述步骤(工作表的数量可以根据输入而变化,所以我相信可以循环执行,但我不知道如何操作)
  • to copy the output of the step above into sheet 1 将上述步骤的输出复制到工作表1中

this is my code so far and since i cant do loop i did it manualy for sheet2 and sheet3. 到目前为止,这是我的代码,由于我无法执行循环,因此我手动完成了sheet2和sheet3的操作。 i get stuck in step 2 我陷入了步骤2

here is the code that've been modified taken from @NEOman' code 这是从@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 know my code is a mess and i wrote comment in every step i did so that i know what the codes are doing. 我知道我的代码很乱,我在每个步骤中都写了注释,以便我知道代码在做什么。 i use different code for column I and J but neither works :(. any help will be appreciated, thanks in advance for your attention. 我为I和J列使用了不同的代码,但均无效:(。任何帮助将不胜感激,在此先感谢您的关注。

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

the code must be run in every sheet (except sheet1) manualy, so im still trying to make the code run from sheet1 but work on any other sheet in same workbook. 该代码必须在每个工作表(除sheet1之外)中手动运行,因此我仍然试图使该代码从sheet1运行,但可以在同一工作簿中的任何其他工作表上运行。 any help will be appreciated, thanks in advance for your attention. 任何帮助将不胜感激,在此先感谢您的关注。

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