Rob
Rob

Reputation: 1

Attempting to Write a Loop in VBA

I am currently trying to write a short loop to condense a list of received items into a concise itemized report. I scan the barcode of each item I receive and it goes into Column A, if there is a quantity of more than 1 it goes into Column B.

Here is my thought process in order to remove all duplicates of items in column A and combine their totals in B:

I know N will need to be reduced after each row deletion and I am pretty new to VBA so I always have trouble writing loops.

Any suggestions at pages to look at or simple structures I could use in my code would be greatly appreciated.

EDIT: Trying to turn table 1 into table 2

Table 1 ----------------------------- Table 2

Column A      Column B    |    Column A     Column B
11233                     |    11233         4
11233          2          |    9987          7
9987                      |    7452          1
11233                     |
9987           6          |
7452                      |

Upvotes: 0

Views: 205

Answers (1)

Abe Gold
Abe Gold

Reputation: 2357

Sub Summator()
ActiveSheet.Columns("A:B").Sort Key1:=ActiveSheet.Range("A2"), Order1:=xlAscending, Header:=xlGuess
lastRow = Range("A65000").End(xlUp).Row
For i = 1 To lastRow
    If Cells(i, 2) = "" Then Cells(i, 2) = 1
Next i
For i = lastRow To 2 Step -1
    If Cells(i, 1) = Cells(i - 1, 1) Then
        Cells(i - 1, 2) = Cells(i - 1, 2) + Cells(i, 2)
        Cells(i, 2).EntireRow.Delete
    End If
Next i
End Sub

Upvotes: 1

Related Questions