Paste data from old listobject to new

neodjandre

Well-known Member
Joined
Nov 29, 2006
Messages
950
Office Version
  1. 2019
Platform
  1. Windows
I am trying to paste data from old listobject to a new listobject and have come with this sub:

Code:
Sub san_import(owb As Workbook, nwb As Workbook, sha As Worksheet, lis As String, col As Long, res As Long)
Dim orng As ListObject
Dim nrng As ListObject
Dim r_fix As Long
Dim c_fix As Long

With owb
    Set orng = sha.ListObjects(lis)
End With

With nwb
    Set nrng = sha.ListObjects(lis)
End With

r_fix = orng.ListRows.Count - nrng.ListRows.Count
c_fix = orng.ListColumns.Count - nrng.ListColumns.Count

If r_fix > 0 Then
    nrng.DataBodyRange.EntireRow.Resize(r_fix).Insert
End If

If c_fix > 0 Then
    nrng.DataBodyRange.EntireColumn.Resize(c_fix).Insert
End If
 
nrng.DataBodyRange.Columns(col).Resize(, res).ClearContents
nrng.Range.Offset(1).Resize(orng.ListRows.Count).Columns(col).Resize(, res).Value2 = orng.DataBodyRange.Columns(col).Resize(, res).Value2
 
End Sub

However, there is a problem with the last line, as the new listobject remains empty. i.e. the old data is not being pasted successfully even if I do not get any errors in vba. Any ideas on how to fix this?

many thanks,
Andrew
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Code:
Sub san_import(owb As Workbook, nwb As Workbook, sha As Worksheet, lis As String, col As Long, res As Long, Optional ByVal off As Long = 0)
Dim orng As ListObject
Dim nrng As ListObject
Dim r_fix As Long
Dim c_fix As Long
Dim i, o As Long

With owb
owb.Activate
sha.Activate
    Set orng = sha.ListObjects(lis)
    orng.AutoFilter.ShowAllData
End With

With nwb
nwb.Activate
sha.Activate
    Set nrng = sha.ListObjects(lis)
    nrng.AutoFilter.ShowAllData
End With

r_fix = orng.ListRows.Count - nrng.ListRows.Count
c_fix = orng.ListColumns.Count - nrng.ListColumns.Count

With nwb
    If r_fix > 0 Then
        For i = 1 To r_fix
           nrng.ListRows.Add AlwaysInsert:=True
        Next i
    End If
    
    If c_fix > 0 Then
        For o = 1 To c_fix
           nrng.ListColumns.Add
        Next o
    End If
    
    sha.Activate
    sha.Range("A1").Select
End With

nrng.DataBodyRange.Offset(off).ClearContents
nrng.DataBodyRange.Offset(off).Columns(col).Resize(, res).Value2 = "test_new_values"

With owb
owb.Activate
sha.Activate
orng.DataBodyRange.Offset(off).Columns(col).Resize(, res).Value2 = "test_old_values"
End With

Application.StatusBar = "Processing " & sha.Name & "..."
End Sub

I was able to pinpoint the problem. The macro works fine in the same workbook. However, for some reason when it comes to two different workbooks, the macro doesn't correctly set "orng" and as such I am getting blank results. The above code is just for testing purposes, which shows that the old_values are not written to the old_table. Any help is much welcomed
 
Upvote 0

Forum statistics

Threads
1,217,402
Messages
6,136,413
Members
450,010
Latest member
Doritto305

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