Help with this loop only first row works

kapela2017

New Member
Joined
Oct 16, 2022
Messages
34
Office Version
  1. 365
Platform
  1. Windows
Greetings, I have a problem relating these two tables, using the "ID" column I did it through a loop, it should take the data from the "Source Table" according to the ID and take it to the "Goal Table", but it only relates the first row the other rows of the "Goal Table" remain in their fields are empty, below I leave the 2 tables and the code any help would be greatly appreciated

SolutionXLookup.xlsm
ABCD
1IDPRODUCTWEIGHTCOST
21001APPLE0,2 KG2,3$
31002PEAR4,5 KG3,1$
41003PEACH5.4 KG2,1$
51004STRAWBERRY2,9 KG3,4$
61005CANTAOLUPE49 KG4,1$
71006MANGO2.4 KG3,2$
81007ORANGE5,9 KG1,4$
91008Banana8,5 KG3,2$
SourceData


SolutionXLookup.xlsm
ABCDEFGH
1IDPRODUCTWEIGHTCOST
21007APPLE5,9 KG1,4$
31002PEAR
41003PEACH
51004STRAWBERRY
6
7
8
9
10
GoalTable




VBA Code:
[CODE=vba][CODE=vba]Option Explicit

Sub CopyData()

Dim wsSourceData As Worksheet
Dim wsGoalTable As Worksheet

Set wsSourceData = ThisWorkbook.Sheets("SourceData")
Set wsGoalTable = ThisWorkbook.Sheets("GoalTable")

Dim SrcTable As ListObject
Set SrcTable = wsSourceData.ListObjects("Source") 'rename

Dim GoalTable As ListObject
Set GoalTable = wsGoalTable.ListObjects("Goal")


Dim a As Object
Dim b As Object
Dim SrcLen As Integer
Dim CounterSrc As Integer
Dim CounterGoal As Integer


SrcLen = SrcTable.ListColumns("ID").DataBodyRange.Rows.Count

CounterSrc = 1
CounterGoal = 1

For Each a In GoalTable.ListColumns("ID").DataBodyRange

    For Each b In SrcTable.ListColumns("ID").DataBodyRange
        If Str(a) = Str(b) Then
            GoalTable.DataBodyRange(CounterGoal, 3).Value = SrcTable.DataBodyRange(CounterSrc, 3).Value 'Weight
            GoalTable.DataBodyRange(CounterGoal, 4).Value = SrcTable.DataBodyRange(CounterSrc, 4).Value 'Cost
        End If
        CounterSrc = CounterSrc + 1
    Next

CounterGoal = CounterGoal + 1
Next


End Sub
[/CODE][/CODE]
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi,
see if this update to your code does what you want

VBA Code:
Option Explicit

Sub CopyData()
    Dim SrcTable    As ListObject, GoalTable As ListObject
    Dim SourceArr   As Variant, ID As Variant, m As Variant
    Dim i           As Long
    
    With ThisWorkbook
        Set SrcTable = .Worksheets("SourceData").ListObjects("Source")
        Set GoalTable = .Worksheets("GoalTable").ListObjects("Goal")
    End With
    
    SourceArr = SrcTable.DataBodyRange.Value
    
    For i = 1 To UBound(SourceArr, xlRows)
        ID = SourceArr(i, 1)
        m = Application.Match(ID, GoalTable.ListColumns("ID").DataBodyRange, 0)
        If Not IsError(m) Then
            With GoalTable
                .DataBodyRange(CLng(m), 3).Value = SourceArr(i, 3)
                .DataBodyRange(CLng(m), 4).Value = SourceArr(i, 4)
            End With
        End If
    Next i
    
End Sub

Dave
 
Upvote 0
Solution
Hi,
see if this update to your code does what you want

VBA Code:
Option Explicit

Sub CopyData()
    Dim SrcTable    As ListObject, GoalTable As ListObject
    Dim SourceArr   As Variant, ID As Variant, m As Variant
    Dim i           As Long
   
    With ThisWorkbook
        Set SrcTable = .Worksheets("SourceData").ListObjects("Source")
        Set GoalTable = .Worksheets("GoalTable").ListObjects("Goal")
    End With
   
    SourceArr = SrcTable.DataBodyRange.Value
   
    For i = 1 To UBound(SourceArr, xlRows)
        ID = SourceArr(i, 1)
        m = Application.Match(ID, GoalTable.ListColumns("ID").DataBodyRange, 0)
        If Not IsError(m) Then
            With GoalTable
                .DataBodyRange(CLng(m), 3).Value = SourceArr(i, 3)
                .DataBodyRange(CLng(m), 4).Value = SourceArr(i, 4)
            End With
        End If
    Next i
   
End Sub

Dave
Hello friend, your code works perfectly, thank you very much for your valuable contribution...
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top