+ Reply to Thread
Results 1 to 3 of 3

Open File / Copy Sheet / Break Links / Save As

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-06-2017
    Location
    Europe
    MS-Off Ver
    2013
    Posts
    123

    Open File / Copy Sheet / Break Links / Save As

    Hello all,

    I need some help with a macro, so if you have a few minutes, please help:
    - i have the following macro in a worksheet and what is does is: it opens a workbook "XXX", update the links, copy the sheet "ABC" to a new file, add 7 days to the date found on cell J7, break links and save the new file on desktop.
    - return to the original file, copy again the sheet ABC to a new workbook, add 14 to the date in J7, break links and save the new file on desktop.

    Everything is working just fine when executed normally, but when i move the code to Personal.xlsB, the resulted dates are not correct.
    I suspect that beeing executed fro Personal.xlsb workbook, i should declare some other variables?

    Could you please help?
    Thank you so much in advance.


    This is the code:

    Sub TEST()
        Dim aLinksArray As Variant
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        'Application.Calculation = xlCalculationManual
        Application.Workbooks.Open Filename:="C:\Users\user\xxx.xlsx", UpdateLinks:=True
        Application.DisplayAlerts = True
    
    
        Sheets("ABC").Select
        Sheets("ABC").Copy
        Range("J7").Value = DateAdd("d", 7, CDate(Range("J7")))
    
        aLinksArray = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
        
        Do Until IsEmpty(aLinksArray)
            ActiveWorkbook.BreakLink Name:=aLinksArray(1), Type:=xlLinkTypeExcelLinks
            aLinksArray = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
        Loop
        
         Application.ScreenUpdating = True
         Application.DisplayAlerts = True
         Application.Calculation = xlCalculationAutomatic
        
        Range("I9:I200").Select
        Selection.ClearContents
    
    
        ActiveWorkbook.SaveAs Filename:= _
            Environ("userprofile") & "\Desktop\" & Format(Date, "YYMMDD") & "_KW" & Range("G5").Value & ".xlsx"
        ActiveWindow.Close
        
    
    
        ActiveWorkbook.Sheets("ABC").Select
        Sheets("ABC").Copy
        Range("J7").Value = DateAdd("d", 14, CDate(Range("J7")))
    
        aLinksArray = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
        
        Do Until IsEmpty(aLinksArray)
            ActiveWorkbook.BreakLink Name:=aLinksArray(1), Type:=xlLinkTypeExcelLinks
            aLinksArray = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
        Loop
        
         Application.ScreenUpdating = True
         Application.DisplayAlerts = True
         Application.Calculation = xlCalculationAutomatic
        
        Range("I9:I200").Select
        Selection.ClearContents
    
    
        ActiveWorkbook.SaveAs Filename:= _
            Environ("userprofile") & "\Desktop\" & Format(Date, "YYMMDD") & "_KW" & Range("G5").Value & ".xlsx"
        ActiveWindow.Close
            'Application.ScreenUpdating = True
         MsgBox "Files are saved on Desktop."
        ActiveWorkbook.Close savechanges:=False
        Application.ScreenUpdating = True
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    08-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2007, 2010, 2013, 2016, Office 365
    Posts
    492

    Re: Open File / Copy Sheet / Break Links / Save As

    Hi

    Although Excel displays dates in whatever format you prefer, the actual cell contents are a numeric value of 'days'.
    So you could simplify your code by replacing...
    Range("J7").Value = DateAdd("d", 7, CDate(Range("J7")))
    with..
    [J7] = [J7] + 7

    and similarly..
    Range("J7").Value = DateAdd("d", 14, CDate(Range("J7")))
    with..
    [J7] = [J7] + 14

    zeddy

  3. #3
    Forum Contributor
    Join Date
    10-06-2017
    Location
    Europe
    MS-Off Ver
    2013
    Posts
    123

    Re: Open File / Copy Sheet / Break Links / Save As

    Thank you for your answer zeddy. I made the changes you suggested.
    Right now i am trying to see, why is that when i run the code from the Visual Basic Editor everything is ok, but when i run it from personal workbook, the results are not correct...

+ 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. macro to save temporary copy if original file is open
    By thoandros in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-03-2020, 05:48 AM
  2. Macro to Open Excel file, Update links, and Save
    By harry1013 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-01-2017, 01:17 PM
  3. Replies: 1
    Last Post: 06-09-2016, 12:05 PM
  4. Want to open file, copy filtered data to new workbook and at save as new file
    By satishdu in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-25-2014, 12:39 PM
  5. Save file as copy while still having original still open
    By djblois1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-12-2013, 03:40 PM
  6. Button to save sheet and open as new file
    By rlsublime in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-07-2012, 03:00 PM
  7. Open Access File & Save Copy As Different Name
    By pr4t3ek in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-19-2008, 06:49 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