Well, I decided to start from scratch and create a blank workbook with sheet1 and sheet2 and see if I could steal/modify some looping code to see if I could get the logic into some rough newbie code. I made a simple table on both sheets where sheet 1 has the item list and expanding qty columns and sheet2 has the lookup list with the pasted inv values to be carried over. It appears that it works correctly in this basic format, so I should be able to apply it to the existing file with a few modifications. Here is the basic code that seems to work, though it is probably not as clean as it should be:
Sub Test()
Dim rngTargetA As Range, rngTargetB As Range
Dim Counter1 As Long, Counter2 As Long
Dim ws As Worksheet, ws1 As Worksheet
Dim the_col As Integer
the_col = 2
Set ws = Application.Worksheets("Sheet1")
Set ws1 = Application.Worksheets("Sheet2")
Counter1 = 2 ' start at row 2 in sheet1
Counter2 = 2 ' start at row 2 in sheet2
Set rngTargetA = ws.Range("A" & Counter1)
Set rngTargetB = ws1.Range("A" & Counter2)
Do While Not IsEmpty(rngTargetA.Value)
Do While Not IsEmpty(rngTargetB.Value)
If rngTargetB.Value = rngTargetA.Value Then
FindValue = ws1.Range("B" & Counter2).Value
try_again:
If ws.Cells(Counter1, the_col) = "" Then
ws.Cells(Counter1, the_col) = FindValue
Else
'offset column until cell is blank
the_col = the_col + 1
GoTo try_again
End If
Counter2 = 2
GoTo next_item
End If
'go to next item on sheet2
Counter2 = Counter2 + 1
Set rngTargetB = ws1.Range("A" & Counter2)
Loop
next_item:
Counter1 = Counter1 + 1
Set rngTargetA = ws.Range("A" & Counter1)
Loop
End Sub
Bookmarks