Copy range of last entered data from another workbook

lpking2005

Board Regular
Joined
Mar 21, 2011
Messages
140
hi,

I currently have 2 seperate workbooks,

I need to copy a range from workbook1 into workbook2.

The problem for me is that people are constantly having to add information to workbook1, so basically i need excel to find and copy the last 5 entries and paste into workbook2.

ive been playing with:

Code:
Range("L500").End(xlUp).select

but i dont know how to use it properly.

any help would mean alot!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
  • Is the code in the source wb (workbook1) or the destination wb (workbook2)?
  • What are the sheet names (or better, the sheets' codenames) in source and destination workbooks.
  • Are we copying only the last five cells (rows) of data from Col L?
 
Upvote 0
sorry for the confusion, i will start again in beter detail.

workbook1 is called ("403 Infohub")
workbook2 is called ("Corrective Actions")

I have a commandbutton called "UPDATE" in workbook1.
When the user clicks this, it should open workbook2, then
copy a range within the last 5 rows that have info entered into them from sheetname ("Mouldings").

Then paste this info into workbook1, sheetname ("RECENT ISSUES")

hope that helps.
 
Upvote 0
Try:

Rich (BB code):
Option Explicit
    
Sub exa()
Dim wbSource            As Workbook
Dim wks                 As Worksheet
Dim wksSource           As Worksheet
Dim strSourceShCodename As String
Dim strPath             As String
Dim strSourceWBName     As String
Dim lLRow               As Long
Dim lLRowNegOffset      As Long
    
    '// Change to suit                                                                  //
    strSourceWBName = "Corrective Actions.xls"
    strPath = ThisWorkbook.Path & "\"
    
    '// I used the sheets' codenames to locate them, rather than the sheets' tab names  //
    '// to eliminate issues with changes to a sheet's name (on the tab).                //
    strSourceShCodename = "shtMouldings"
    
    '// Just in case the desired wb isn't there.                                        //
    On Error Resume Next
    Set wbSource = Workbooks.Open(strPath & strSourceWBName, , True)
    On Error GoTo 0
    
    If Not wbSource Is Nothing Then
        
        '// Find the worksheet with the correct codename and set a reference to it.     //
        For Each wks In wbSource.Worksheets
            If wks.CodeName = strSourceShCodename Then
                Set wksSource = wks
                Exit For
            End If
        Next
        '// Ensure the sheet has not been deleted.                                      //
        If wksSource Is Nothing Then
            MsgBox "Unable to find source sheet.", vbOKOnly + vbInformation, vbNullString
            wbSource.Close False
            Exit Sub
        End If
    Else
        MsgBox "Unable to locate wb..", vbOKOnly Or vbInformation, vbNullString
        Exit Sub
    End If
    
    With wksSource
        '// I still wasn't sure if we are only grabbing the cells in L Col, but this    //
        '// should take the vals from the last five rows in L col and plunk them into   //
        '// the next available cells in col A of the destination wb.                    //
        lLRow = .Cells(.Rows.Count, "L").End(xlUp).Row
        lLRowNegOffset = Application.Max(2, lLRow - 4)
        
        shtRecentIssues.Cells(shtRecentIssues.Rows.Count, "A").End(xlUp).Offset(1) _
                       .Resize(Range(.Cells(lLRowNegOffset, "L"), .Cells(lLRow, "L")).Rows.Count).Value _
                            = Range(.Cells(lLRowNegOffset, "L"), .Cells(lLRow, "L")).Value
        
        .Parent.Close False
    End With
End Sub

Hope that helps,

Mark
 
Upvote 0
Thanks for trying this GTO!

Sorry i didnt reply sooner, i had a few days off work.

I have pasted the code in and attached to a commandbutton, i made all the neccesary changes.

I had to add the following because of a "shtRecentIssues not defined" error:
Code:
Dim shtRecentIssues     As Worksheet

Changed some code to the precise filepaths.

At the moment it opens the workbook, but has trouble finding the source sheet.

Just to clarify, i want it to pull data from "B : L" in the last 5 rows.


this is what i have so far:

Code:
Option Explicit
Private Sub CommandButton1_Click()

Dim wbSource            As Workbook
Dim wks                 As Worksheet
Dim wksSource           As Worksheet
Dim strSourceShCodename As String
Dim strPath             As String
Dim strSourceWBName     As String
Dim lLRow               As Long
Dim lLRowNegOffset      As Long
Dim shtRecentIssues     As Worksheet

    
    '// Change to suit                                                                  //
    strSourceWBName = "Mouldings, Liquids & Powders Outstanding CA's.xls"
    strPath = "T:\QA Shared Data\5 D Reports" & "\"
    
    '// I used the sheets' codenames to locate them, rather than the sheets' tab names  //
    '// to eliminate issues with changes to a sheet's name (on the tab).                //
    strSourceShCodename = "shtMOULDINGS"
    
    '// Just in case the desired wb isn't there.                                        //
    On Error Resume Next
    Set wbSource = Workbooks.Open(strPath & strSourceWBName, , True)
    On Error GoTo 0
    
    If Not wbSource Is Nothing Then
        
        '// Find the worksheet with the correct codename and set a reference to it.     //
        For Each wks In wbSource.Worksheets
            If wks.CodeName = strSourceShCodename Then
                Set wksSource = wks
                Exit For
            End If
        Next
        '// Ensure the sheet has not been deleted.                                      //
        If wksSource Is Nothing Then
            MsgBox "Unable to find source sheet.", vbOKOnly + vbInformation, vbNullString
            wbSource.Close False
            Exit Sub
        End If
    Else
        MsgBox "Unable to locate wb..", vbOKOnly Or vbInformation, vbNullString
        Exit Sub
    End If
    
    With wksSource
        '// I still wasn't sure if we are only grabbing the cells in L Col, but this    //
        '// should take the vals from the last five rows in L col and plunk them into   //
        '// the next available cells in col A of the destination wb.                    //
        lLRow = .Cells(.Rows.Count, "L").End(xlUp).Row
        lLRowNegOffset = Application.Max(2, lLRow - 4)
        
        shtRecentIssues.Cells(shtRecentIssues.Rows.Count, "A").End(xlUp).Offset(1) _
                       .Resize(Range(.Cells(lLRowNegOffset, "L"), .Cells(lLRow, "L")).Rows.Count).Value _
                            = Range(.Cells(lLRowNegOffset, "L"), .Cells(lLRow, "L")).Value
        
        .Parent.Close False
    End With


End Sub
 
Upvote 0
is there any chance someone could look into this please?

Im really stumped with this! :(


Sorry, I missed seeing this. I did mention in the code " '// Find the worksheet with the correct codename and set a reference to it. //", but that was probably less than clear on my part.

A worksheet's CodeName is not the name on the sheet's tab, but rather it's "real" name or object name or however you want to think of it. Like when you insert a new UserForm, and Excel names it 'UserForm1'.

So, we just need to change the CodeName of 'RECENT ISSUES'. Select the sheet, right-click the sheet tab and select View Code. Look in the Properties Window of VBIDE; this is usually in the lower left of the screen.

You'll see two Name properties. Change the top one.
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,240
Members
452,898
Latest member
Capolavoro009

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