Thanks to Doug Glancy for his suggestions.
The original question dealt with the loading of a .csv into a listobject. I ended up loading the data from the csv into a listobject on a new sheet. I was then able to merge a source listobject into a target listobject while maintaining the additional columns (and formulas) the target maintained using the procedure below. This procedure clears the data that is not needed, removes rows if the source contains less rows than the target, copies the data, and resizes the listobject.
Assumptions within this solution
- source list object contains some number of columns less than the target
- both list objects contain a single header row (I did not test anything more than a single header row)
Private Sub CopyTableData(loSource As Excel.ListObject, loTarget As Excel.ListObject)
Dim lSourceRowCount As Long
With loTarget
If .DataBodyRange.Rows.Count <> loSource.DataBodyRange.Rows.Count Then
' clear of target area should clear target num rows and source num columns
.Range(.Cells(1,1).Address, .Cells(.DataBodyRange.Rows.Count, lSource.RefersToRange.Columns.Count)).Clear
' clear rows if source has less than target
If .DataBodyRange.Rows.Count > loSource.DataBodyRange.Rows.Count Then
For i = .DataBodyRange.Rows.Count To loSource.DataBodyRange.Rows.Count + 1 Step -1
.DataBodyRange.Rows(i).Clear
Next i
End If
' resize list object
lSourceRowCount = loSource.HeaderRowRange.Rows.Count + _
loSource.DataBodyRange.Rows.Count
.Resize .Range.Cells(1).Resize(lSourceRowCount, .Range.Columns.Count)
End If
loSource.DataBodyRange.Copy .DataBodyRange.Cells(1)
End With
End Sub
Thanks again.
Resize
method to get the right number of rows and copy in the new columns. I have a somewhat related post on my blog. – Doug Glancy