excel - VBA Excel 2007 : Need to loop copy and loop count number except zero every row above

标签 excel vba loops sum countif

我是 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
    

    关于excel - VBA Excel 2007 : Need to loop copy and loop count number except zero every row above,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27699308/

    相关文章:

    r - 如何将多个表、数据框、回归结果等写入一个 Excel 文件?

    Excel VBA四舍五入与 double

    VBA for Excel 2010/2013 - 如何识别系统区域设置

    excel - 复制和粘贴的宏扩展不起作用

    java - 我的程序不允许我调用不同类的方法

    python - 如何让我的明确循环每行打印一个

    Java Swing 'game loop' 最佳实践

    ms-access - VBA 文件打开速度很慢

    excel - 返回到按下按钮的工作表

    web-services - 使用 SOAP 从 VBA 调用 Web 服务