Forum Discussion

mousepad1254's avatar
mousepad1254
Copper Contributor
Jun 16, 2022

Excel coding help comparing values and matching data

I am trying to code this excel to help with a work project. The objective is to check if any date in column A matches with another date in that column. If it matches It is supposed to add the numbers in column C that correspond to the matching dates in column A. The end result is column D which displays the number of apples in that day total. The highlighted portions are matching days. I have included a picture of this excel 

 

3 Replies

  • mousepad1254 

    Sub apples()
    
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim row1 As Integer
    
    Range("D2:D1000").Clear
    k = Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 2 To k
    
    If Cells(i, 1).Value = Cells(i + 1, 1).Value Then
    j = j + Cells(i, 3).Value
    
    If Cells(i, 1).Value <> Cells(i + 2, 1).Value Then
    j = j + Cells(i + 1, 3).Value
    Cells(i + 1, 4).Value = j
    j = 0
    Else
    
    End If
    
    Else
    End If
    
    Next i
    
    End Sub

    Maybe with these lines of code. In the attached file you can click the button in cell E2 to start the macro.

    • mousepad1254's avatar
      mousepad1254
      Copper Contributor
      Thank you very much that worked. Is there a way to also still display the values that are not the same date in the same column?
      • OliverScheurich's avatar
        OliverScheurich
        Gold Contributor

        mousepad1254 

        Sub apples()
        
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim row1 As Integer
        
        Range("D2:D1000").Clear
        k = Range("A" & Rows.Count).End(xlUp).Row
        
        For i = 2 To k
        
        If Cells(i, 1).Value = Cells(i + 1, 1).Value Then
        j = j + Cells(i, 3).Value
        
        If Cells(i, 1).Value <> Cells(i + 2, 1).Value Then
        j = j + Cells(i + 1, 3).Value
        Cells(i + 1, 4).Value = j
        j = 0
        Else
        
        End If
        
        Else
        
        If Cells(i + 1, 1).Value <> Cells(i + 2, 1).Value Then
        Cells(i + 1, 4).Value = Cells(i + 1, 3).Value
        
        Else
        End If
        
        End If
        
        Next i
        
        End Sub

        Maybe with this code. In the attached file you can click the button in cell F2 to start the macro.