Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

excel - VBA nesting loops to calculate multiple employee hours

I have been working on getting a timesheet macro that will take a data dumps and make do a few things. Ultimately I am not familiar with the syntax of VBA and have got close however am needing help with getting this finished. Below will be my code and comments where I am working on code as well as a screenshot for reference of the spreadsheet.

1 My question is how do I properly write the syntax using vars? For instance in this line of code: If IsNumeric(Cells("Fr").Value) Then I am geting errors and am unsure how I would enter the r value from the loop. This applies to a few of the other lines I was getting errors for but didn't know how to use r to identify a row.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.6k users

...