3

In my Excel worksheet I have several values I need to compare and sum up in case defined criteria match.

The worksheet contains these information:

Name(A), Date(B), Hours worked(C), other information(D-H).

Via VBA I want to check if Hours worked exceeds the value "10". If it does then the code needs to compare if the Name in the previous row equals the Name in the current AND the Date of both rows equal each other.

If all these conditions are true the Hours worked should be summed up and the result should be copied to worksheet 2. Also the needed information like Name, Date and other information should be copied.

For now I tried this:

Sub check_Click()

    Dim s1 As Worksheet, s2 As Worksheet
    Dim N As Long, i As Long, p As Long

    Set s1 = Sheets(1)
    Set s2 = Sheets(2)

    N = s1.Cells(s1.Rows.Count, "C").End(xlUp).Row
    p = 1

    For i = 1 To N
        If IsNumeric(s1.Range("C" & i)) And s1.Cells(i, "C").Value < 10 Then
            Next i
        ElseIf s1.Cells(i, "B").Value = s1.Cells(i - 1, "B").Value And s1.Cells(i, "A").Value = s1.Cells(i - 1, "A").Value Then
            s1.Range(Cells(i, "A"), Cells(i, "C")).Copy s2.Cells(p + 5, 1)                
        End If
End Sub

As you might see the code isn't working - unfortunate.

I hope someone can light my way.

The trickiest part is to compare the previous row and sum up the hours.

Thanks in advance

2 Answers 2

2

The code is not proper. Next i cannot be used inside If ... Then.

Because of lack continue in VBA you have to change condition also (or use Goto, but this is not my preferred solution):

Sub check_Click()
    Dim s1 As Worksheet, s2 As Worksheet
    Dim N As Long, i As Long, p As Long

    Set s1 = Sheets(1)
    Set s2 = Sheets(2)

    N = s1.Cells(s1.Rows.Count, "C").End(xlUp).Row
    p = 1

    For i = 1 To N
        If IsNumeric(s1.Range("C" & i)) And s1.Cells(i, "C").Value >= 10 Then
            If s1.Cells(i, "B").Value = s1.Cells(i - 1, "B").Value And s1.Cells(i, "A").Value = s1.Cells(i - 1, "A").Value Then
                s1.Range(Cells(i, "A"), Cells(i, "C")).Copy s2.Cells(p + 5, 1)
            End If
        End If
    Next i      
End Sub

EDIT:

Because values are compared with previous row, for loop neds to start from 2.

Sub check_Click()
    Dim s1 As Worksheet, s2 As Worksheet
    Dim N As Long, i As Long, p As Long

    Set s1 = Sheets(1)
    Set s2 = Sheets(2)

    N = s1.Cells(s1.Rows.Count, "C").End(xlUp).Row
    p = 1

    For i = 2 To N ' Iterate from second row
        If IsNumeric(s1.Range("C" & i)) And s1.Cells(i, "C").Value >= 10 Then
            If s1.Cells(i, "B").Value = s1.Cells(i - 1, "B").Value And s1.Cells(i, "A").Value = s1.Cells(i - 1, "A").Value Then
                s1.Range(Cells(i, "A"), Cells(i, "C")).Copy s2.Cells(p + 5, 1)
            End If
        End If
    Next i      
End Sub
9
  • All I get is runtime error 1004...I need to figure this out
    – edorius
    Jun 2, 2017 at 7:52
  • Additional problem is that you are comparing with previous row - therefore For loop needs to start from 2. Jun 2, 2017 at 8:02
  • 1
    On my simple example it is working well. You have to debug it to see if something in data is not causing error. I.e. in which row and column error occurs, what data are in those rows.Maybe you can post sample sheet (few rows) that the error could be reproduced. Jun 2, 2017 at 8:50
  • The debug shows that line If s1.Cells(i, "B").Value = s1.Cells(i - 1, "B").Value And s1.Cells(i, "A").Value = s1.Cells(i - 1, "A").Value Then s1.Range(Cells(i, "A"), Cells(i, "C")).Copy s2.Cells(p + 5, 1) is causing errors
    – edorius
    Jun 2, 2017 at 9:26
  • Please add to post at least 2-3 lines with sample data. You can also check which individual element is failing by mark i.e. s1.Cells(i, "B").Value and press Shift+F9. By same technique you can check what row (i) and other parameters. Jun 2, 2017 at 10:30
1

Your Next i is in a wrong place. It should be after all the If statements.

I think comparing the values is done correctly.

If you have trouble copying hours summed just copy the entire row to sheet2 first and then separately update the hours worked cell with something like this:

Worksheets("sheet2").Cells(i,3).Value = Cells(i,3).Value + Cells(i-1,4).Value

Of course replace with the correct cell coordinates.

2
  • hmm, but how can I instruct the code to check the next row?
    – edorius
    Jun 2, 2017 at 7:36
  • Doesn't this part of your code already do that: If s1.Cells(i, "B").Value = s1.Cells(i - 1, "B").Value
    – Alluton
    Jun 2, 2017 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.