+ Reply to Thread
Results 1 to 5 of 5

Marco to update sheet first and then copy

Hybrid View

  1. #1
    Registered User
    Join Date
    09-13-2012
    Location
    GA, USA
    MS-Off Ver
    2010
    Posts
    48

    Marco to update sheet first and then copy

    Hi,

    I'm looking for a macro, that first updates a sheet located in a different file and then copies the updated data to paste in a report file. Below is a macro that I have got so far, but this macro copies data as is without updating Raw data sheet. The Raw data sheet is updated using macro button.

    Any help greatly appreciated. Thanks!
    Sub GetRaw()
    
    Dim wb As Workbook
    Dim activeWB As Workbook
    Dim FilePath As String
    Set activeWB = Application.ActiveWorkbook
    Dim lastrow As Long
    
       Application.ScreenUpdating = False
       Application.DisplayAlerts = False
       Application.AskToUpdateLinks = False
    
    'Update Report sheet by copying updated data from Raw
        Sheets("Report 1").Select
        Columns("A:K").Select
        Selection.Delete Shift:=xlToLeft
        
    FilePath = "\\C:\ Data\Raw Data Lookup.xlsm"
       
       Set wb = Application.Workbooks.Open(FilePath, ReadOnly:=True)
        Sheets("Raw1").Select
        Columns("A:K").Select
        Selection.Copy
        activeWB.Activate
        Worksheets("Report").Activate
        Range("A1").Select
        ActiveSheet.Paste
       wb.Close False
    
    'Breaks all links from importing sheets
    Dim Links As Variant
    Dim i As Long
    Links = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
    For i = 1 To UBound(Links)
    ActiveWorkbook.BreakLink _
        Name:=Links(i), _
        Type:=xlLinkTypeExcelLinks
    Next i
    
       Application.ScreenUpdating = True
       Application.DisplayAlerts = True
       Application.AskToUpdateLinks = True
    Last edited by alansidman; 05-07-2018 at 01:59 PM.

  2. #2
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,493

    Re: Marco to update sheet first and then copy

    All your macro does is open the raw data sheet and immediately copy the data. I do not see any prove that the raw data is updated. You say raw data sheet is updated with a macro button.

    my guess would be you will have to trigger the macro in the raw data sheet "Raw Data Lookup" to update before you copy.
    I do not know the name of that macro but you can call the macro to action in your code.

    I put a line in red where you should call the macro
    I also cleanup your code from not needed selects and activates.

    Sub GetRaw()
    
    Dim wb As Workbook
    Dim activeWB As Workbook
    Dim FilePath As String
    Set activeWB = Application.ActiveWorkbook
    Dim lastrow As Long
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.AskToUpdateLinks = False
    
        'Update Report sheet by copying updated data from Raw
        Sheets("Report 1").Columns("A:K").Delete Shift:=xlToLeft
        
        FilePath = "\\C:\ Data\Raw Data Lookup.xlsm"
        Set wb = Application.Workbooks.Open(FilePath, ReadOnly:=True)
         Call ....   'put name update macro from opened file here (replace the dots with name)
         Sheets("Raw1").Columns("A:K").Copy activeWB.Worksheets("Report").Range("A1")
        wb.Close False
    
        'Breaks all links from importing sheets
        Dim Links As Variant
        Dim i As Long
        Links = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
        For i = 1 To UBound(Links)
        ActiveWorkbook.BreakLink _
            Name:=Links(i), _
            Type:=xlLinkTypeExcelLinks
        Next i
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.AskToUpdateLinks = True
    End Sub

  3. #3
    Registered User
    Join Date
    09-13-2012
    Location
    GA, USA
    MS-Off Ver
    2010
    Posts
    48

    Re: Marco to update sheet first and then copy

    You are correct I want to trigger a macro in the "Raw Data Lookup" sheet before I copy the data. I tried your code but its giving me an error, the macro name called out is "UpdateMain" and this is how I called it out...

     Call UpdateMain

  4. #4
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,493

    Re: Marco to update sheet first and then copy

    Ah yes.. have to call a different way for macro's in other workbook..
    try the code below
    Sub GetRaw()
    
    Dim wb As Workbook
    Dim activeWB As Workbook
    Dim FilePath As String
    Set activeWB = Application.ActiveWorkbook
    Dim lastrow As Long
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.AskToUpdateLinks = False
    
        'Update Report sheet by copying updated data from Raw
        Sheets("Report 1").Columns("A:K").Delete Shift:=xlToLeft
        
        FilePath = "\\C:\ Data\Raw Data Lookup.xlsm"
        Set wb = Application.Workbooks.Open(FilePath, ReadOnly:=True)
         Application.Run ("'Raw Data Lookup.xlsm'!UpdateMain") 
         Sheets("Raw1").Columns("A:K").Copy activeWB.Worksheets("Report").Range("A1")
        wb.Close False
    
        'Breaks all links from importing sheets
        Dim Links As Variant
        Dim i As Long
        Links = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
        For i = 1 To UBound(Links)
        ActiveWorkbook.BreakLink _
            Name:=Links(i), _
            Type:=xlLinkTypeExcelLinks
        Next i
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.AskToUpdateLinks = True
    End Sub

  5. #5
    Registered User
    Join Date
    09-13-2012
    Location
    GA, USA
    MS-Off Ver
    2010
    Posts
    48

    Re: Marco to update sheet first and then copy

    Awesome, it worked . It takes longer to run the report though, so it proves that it is triggering the macro in other workbook.

    Thank you!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 02-13-2017, 04:51 PM
  2. copy data from one sheet to annother, format it; Update to refect changes on source sheet
    By simplificated in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-13-2013, 03:45 PM
  3. Replies: 0
    Last Post: 09-10-2013, 08:17 AM
  4. Need Marco to review row for an array of codes and update cell with yes or no
    By Robert.Bristow in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-11-2013, 01:24 PM
  5. Marco to get data and also to update table with amendments?
    By rmharrison in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-04-2012, 11:25 PM
  6. Replies: 7
    Last Post: 12-03-2012, 03:10 AM
  7. marco button to update master sheet from multiple workbooks
    By mikeydaman in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-11-2009, 10:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1