vendredi 20 septembre 2019

Putting a loop inside of an if condition - can it be done?

I'm creating a tool that pulls in data from SQL and then extracts the data depending on dates and users relating to that data. I need this to work in a large range of users.

I have some code that currently has a nested for loop. Using lastrow variables, it checks to see if the value of a cell and the first loop value - for example .range("b" & R) - equals the value of a call and the second loop value - .range("H" & i). In this, there's an if statement e.g

If the date in range("E"&r) = the date in range("G"&i) and lcase(range("A" & r) = lcase(range("H" & y) then...

I need the value of y to change to basically say if G&i = H&y or H&y and so on until it gets to the end of my range.

Sub mysub(sheet As String)

Dim i As Long
Dim r As Long
Dim y As Long
Dim lastRow As Long
Dim lastRow2 As Long
Dim hours As Single
Dim lastRow3 As Long

Application.ScreenUpdating = False

Sheets(sheet).Activate

lastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
lastRow2 = ActiveSheet.Range("AD" & Rows.Count).End(xlUp).Row
lastRow3 = Sheets("mysheet").Range("B" & Rows.Count).End(xlUp).Row

For r = 4 To lastRow2
    For i = 2 To lastRow
        For y = 2 To lastAgentRow
        If ActiveSheet.Range("E" & i).Value = ActiveSheet.Range("AH" & r) And _
            LCase(ActiveSheet.Range("A" & i).Value) = LCase(Sheets("agents").Range("B" & y).Value) Then
            hours = hours + ActiveSheet.Range("D" & i).Value
        End If
        Next y
    Next i
ActiveSheet.Range("AE" & r).Value = hours
ActiveSheet.Range("AE" & r).NumberFormat = "[h]:mm"
hours = 0
Next r


Application.ScreenUpdating = True

End Sub

Any help on this would be a massive help.

Aucun commentaire:

Enregistrer un commentaire