Set destination range to last row of table

MGreeno

New Member
Joined
Mar 10, 2022
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
I am hoping you all can help. I am trying to modify a previously working sub to Copy and Paste a SourceRange into a named table. The table used to be on its own sheet which made the VBA easy but now it has been requested to move the table to a central location sharing a sheet with other tables.
Code I currently have is below, I cannot set the Destination range. Any help would be appreciated as I will use this multiple times in this workbook.

VBA Code:
Sub MonthyVendorMove()
    Dim sourceRange As Range
    Dim destrange As Range
    Dim LR As Integer
    Dim MVTable As ListObject

Set MVTable = Workbooks("On Time Delivery Current.xlsm").Sheets("OTD Tables").ListObjects("MonthlyVendor")
   
'Calls function to find Last Row on Named Table MonthlyVendor
    LR = MVTable.Range.Rows.Count
'ERROR here  Sets Destination Range, Where info will be going
    Set destrange = MVTable.Range("AG" & LR)

'Sets Range to be Copied
    Set sourceRange = Workbooks("On Time Delivery Current.xlsm").Sheets("Weekly Shipments Pivot").Range("A25:C25")
'Copy the Range set in line before
    sourceRange.Copy
'Pastes only Values
    destrange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
             
 MsgBox ("Done")
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try . . .

VBA Code:
Sub MonthyVendorMove()

    Dim sourceRange As Range
    Dim MVTable As ListObject
    Dim newListRow As ListRow

'Sets source Range to be Copied
    Set sourceRange = Workbooks("On Time Delivery Current.xlsm").Sheets("Weekly Shipments Pivot").Range("A25:C25")

'Sets the table to copy to
    Set MVTable = Workbooks("On Time Delivery Current.xlsm").Sheets("OTD Tables").ListObjects("MonthlyVendor")
  
'Adds a new list row
    Set newListRow = MVTable.ListRows.Add

'Copies the source Range
    sourceRange.Copy
    
'Pastes only Values
    newListRow.Range.PasteSpecial Paste:=xlPasteValues
            
    MsgBox ("Done")
 
End Sub

Hope this helps!
 
Upvote 0
Solution

Forum statistics

Threads
1,224,520
Messages
6,179,267
Members
452,902
Latest member
Knuddeluff

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