首页 > 解决方案 > VBA嵌套循环计算多个员工小时

问题描述

我一直在努力获得一个时间表宏,它将进行数据转储并做一些事情。最终我不熟悉 VBA 的语法并且已经接近但是我需要帮助来完成这个。下面将是我正在处理代码的代码和注释以及电子表格参考的屏幕截图。

1 我的问题是如何使用 vars 正确编写语法?例如在这行代码中: If IsNumeric(Cells("Fr").Value) Then 我收到错误并且不确定如何从循环中输入 r 值。这适用于我遇到错误但不知道如何使用 r 来识别行的其他几行。

Sub sum()
    Dim r As Integer, c As Integer, s As Double, t As Integer, g As Integer
    r = 2 'looping var
    c = 3 'looping var
    s = 0 'var for sum
    g = 0
    t = ActiveSheet.UsedRange.Rows.Count                    'var for total rows
    
    
    Do Until r = t
        If Not IsEmpty(Range("Ar").Value) = True Then       'check if user name is present then
                                                            'Detect the next cell that contains data in the user name column 
                                                            'Use that number between the two as a var (g) that will be used to run the embedded looping
                                                            'essentially redefining the other loop each time to account for the different number of clock ins per user
        

            Do Until c = g                                  'Loop for until the next name was detected via var (g)
                If IsNumeric(Cells("Fr").Value) Then        'check if Billable has a number then
                s = s + Range("r, F").Value                 'adds cell value (numbers only) to sum
                c = c + 1                                   'add 1 to the value of c
            Loop                                            'closes embedded loop once values have been added up  
        Range("Fr") = s                                     'Replace Cell (Fr) with the sum value
        s = 0                                               'reset the value of the sum
                                                        
        r = r + 1
    Loop
End Sub

标签: excelvba

解决方案


推荐阅读