I am trying to check if column A row 1 matches column B row 1 and if this matches then a check on Column E row 1 with Column E row 2 needs to be conducted. If all match then a value in column C row 1 needs to be displayed.
If column A row 1 matches column B row 1 and if column E row 1 matches column 2 row 2 then move to row 2 and check if column A row 2 matches column A row 2 if this matches check to see if column E row 1 matches column E row 2. if this matches but below line does not match then sum the two values in column C for row 1 and row 2.
I have this piece of code:
Sub DemoNew()
Dim dict1 As Object
Dim c1 As Variant, k As Variant
Dim currWS As Worksheet
Dim i As Double, lastRow As Double, tot As Double
Dim number1 As Double, number2 As Double, firstRow As Double
Set dict1 = CreateObject("Scripting.Dictionary")
Set currWS = ThisWorkbook.Sheets("Sheet1")
'get last row withh data in Column A
lastRow = currWS.Cells(Rows.count, "A").End(xlUp).Row
'put unique numbers in Column A in dict1
c1 = Range("A2:B" & lastRow)
For i = 1 To UBound(c1, 1)
If c1(i, 1) <> "" Then
'make combination with first 4 characters
dict1(Left(c1(i, 1), 4) & "," & Left(c1(i, 2), 4)) = 1
End If
Next i
'loop through all the numbers in column A
For Each k In dict1.keys
number1 = Split(k, ",")(0)
number2 = Split(k, ",")(1)
tot = 0
firstRow = 0
For i = 2 To lastRow
If k = Left(currWS.Range("A" & i).Value, 4) & "," & Left(currWS.Range("B" & i).Value, 4) Then
If firstRow = 0 Then
firstRow = i
End If
tot = tot + currWS.Range("C" & i).Value
End If
Next i
currWS.Range("D" & firstRow) = tot
Next k
End Sub
I have tried this:
If k = Left(currWS.Range("A" & i).Value, 4) & "," & Left(currWS.Range("B" & i).Value, 4) & (currWS.Range("E" & i).value) Then
but this does not produce what I wanted.
Here is a graphical representation: Example 1
Any suggestions?
Thanks
This do what you need?
Sub DemoNew()
Dim dict1 As Object
Dim c1 As Variant, k As Variant
Dim currWS As Worksheet
Dim i As Double, lastRow As Double, tot As Double
Dim number1 As Double, number2 As Double, firstRow As Double
Dim DataArray
Set dict1 = CreateObject("Scripting.Dictionary")
Set currWS = ThisWorkbook.Sheets("Sheet1")
'get last row withh data in Column A
lastRow = currWS.Cells(Rows.Count, "A").End(xlUp).Row
'put your sheet into an array in memory so that it references faster
DataArray = Range("A2:E" & lastRow)
'loop through the array per your logic
For i = 1 To UBound(DataArray, 1) - 1
If DataArray(i, 1) <> "" And DataArray(i, 1) = DataArray(i, 2) And DataArray(i, 5) = DataArray(i + 1, 5) Then
tot = tot + DataArray(i, 3)
Else
DataArray(i, 4) = DataArray(i, 3)
End If
Next i
DataArray(UBound(DataArray, 1), 4) = DataArray(UBound(DataArray, 1), 3) ' last row will never match the next so set the value in D
'write modified data back into sheet
currWS.Range("A2:E" & lastRow) = DataArray
MsgBox ("Tot is: " & tot)
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.