Cannot transfer data from table to table by referencing column headers

Julesdude

Board Regular
Joined
Jan 24, 2010
Messages
197
Hi there all, please can someone help with a basic triple loop scenario:

I have 2 tables, A and B, one in workbook 1, the other in workbook 2.
Table A is basically table B but with empty columns removed, so you just have columns with rows where values are in. Table B is a blank uncondensed table with all the possible column headers had table A not been condensed.
In VBA I need to find a way of getting Excel to look for the column header codes in Table A, find the value that sits underneath it, then scan the column header row in table B to find a match and then transfer the value from A into B. This has to loop until all column values for that row in table A have been pasted into B.
Then it needs to advance to the next row in table A, or if it is blank then exit. For each new row looked at in table A, this should also start a new row in table B.

So far I have code that does not do anything and I am not sure why:

Code:
DestIndex = 1
For sourcerow = 4 To 250
If Not Workbooks(SourceWorkbookName).Sheets("Sheet 1").Cells(sourcerow, 7) = "" Then
    For sourcecolumn = 9 To 50
        
        If Not Workbooks(SourceWorkbookName).Sheets("Sheet 1").Cells(3, sourcecolumn) = "" Then
            sourcecode = Workbooks(SourceWorkbookName).Sheets("Sheet 1").Cells(3, sourcecolumn)
            Do
                   If Workbooks(DestWorkbookName).Sheets("Sheet 1").Cells(5, DestIndex) = sourcecode Then
                    sourcevalue = Workbooks(SourceWorkbookName).Sheets("Sheet 1").Cells(4, sourcecolumn)
                    Workbooks(DestWorkbookName).Sheets("Sheet 1").Cells(6, DestIndex) = sourcevalue
                    DestIndex = DestIndex + 1
                    Exit Do
                Else
                    DestIndex = DestIndex + 1
                End If
            Loop Until Workbooks(DestWorkbookName).Sheets("Sheet 1").Cells(5, DestIndex) = ""
        Else
        Exit For
        End If
    Next sourcecolumn
Else
Exit For
End If
Next sourcerow
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I scapped the original code and used this instead but still it does nothing

Can anyone help?

Code:
For SourceRow = 4 To 250
If Workbooks(SourceWorkbookName).Sheets("Sheet1").Cells(SourceRow, 7) = "" Then
Exit For
End If
For SourceColumn = 11 To 250
If Workbooks(SourceWorkbookName).Sheets("Sheet1).Cells(3, SourceColumn) = "" Then
Exit For
End If
For DestinationColumn = 1 To 250
If Workbooks(SourceWorkbookName).Sheets("Sheet1").Cells(6, DestinationColumn) = "" Then
Exit For
End If
If Workbooks(SourceWorkbookName).Sheets("Sheet1").Cells(3, SourceColumn) = Workbooks("Dest").Sheets("Tracker").Cells(6, DestinationColumn) Then
Workbooks(SourceWorkbookName).Sheets("Sheet1").Cells(SourceRow, SourceColumn) = Workbooks("Dest").Sheets("Tracker").Cells(6, DestinationColumn)
Exit For
End If
Next DestinationColumn
Next SourceColumn
Next SourceRow
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

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