Results 1 to 6 of 6

switch between open workbooks within macro

Threaded View

  1. #1
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    switch between open workbooks within macro

    in this code a target file is is opened and saved 3 times which is unnnecessary but I dont know how to change it. After the first instance of "paste" in workbook opened by the macro, rather than close it I would just like to instruct the macro to activate the previous worksheet before continuing.

    This system was OK with a small test file but unusable where it is needed

    Any help appreciated

    I tried with this method at line 21 Windows("c:\users\nigel.MACROOMHAULAGE\My Documents\vehicle costing.xlsm").Activate but no success
    Sub Macro2()
    
    If Application.WorksheetFunction.CountA(Range("c4:c330")) = 0 Then
        MsgBox "No Data highlighted!"
        Exit Sub
    End If
    
    
    MsgBox "Make Sure Maintenance File is closed before clicking OK - "
    
    Dim rng As Range
    Dim rfiltered As Range
    Dim path, ws_name As String
    ws_name = ActiveSheet.Name
    ScreenUpdating = False
    
    
        With ActiveWorksheet
        Set rng = Range("a3:t" & Range("a65500").End(xlUp).Row)
    
        rng.AutoFilter Field:=3, Criteria1:="M"
    'MsgBox "1"
          path = Mid(ActiveWorkbook.FullName, 1, Len(ActiveWorkbook.FullName) - Len(ActiveWorkbook.Name))
          
        
        Range("a4:b" & Range("c65500").End(xlUp).Row).Copy
     
    
        
            Workbooks.Open ("c:\users\nigel.MACROOMHAULAGE\My Documents\maintenance.xlsm")
        
                Sheets(ws_name).Activate
                NextRow = Range("B14").End(xlUp).Row + 1
                Range("B" & NextRow).Select
     
        
        
                ActiveSheet.Paste
                Windows("c:\users\nigel.MACROOMHAULAGE\My Documents\vehicle costing.xlsm").Activate
                'ActiveWorkbook.Save
                'ActiveWorkbook.Close
        MsgBox "2"
                    Range("e4:e" & Range("c65500").End(xlUp).Row).Copy
        
                    Workbooks.Open ("c:\users\nigel.MACROOMHAULAGE\My Documents\maintenance.xlsm")
        
                    Sheets(ws_name).Activate
                    NextRow = Range("d14").End(xlUp).Row + 1
                    Range("d" & NextRow).Select
                    
        
                    ActiveSheet.Paste
        
                    ActiveWorkbook.Save
                    ActiveWorkbook.Close
        
        
                Range("g4:j" & Range("c65500").End(xlUp).Row).Copy
        
                    Workbooks.Open ("c:\users\nigel.MACROOMHAULAGE\My Documents\maintenance.xlsm")
        
                    Sheets(ws_name).Activate
                    NextRow = Range("f14:i14").End(xlUp).Row + 1
                    Range("f" & NextRow).Select
                    
        
                    ActiveSheet.Paste
        
        Dim Ans As Long
        
        Ans = MsgBox("Save Update?", vbYesNo)
        
        If Ans = vbYes Then
        ActiveWorkbook.Save
        ActiveWorkbook.Close
        ElseIf Ans = vbNo Then
        ActiveWorkbook.Close
        End If
        
        
    
        
       rng.AutoFilter
       
       Range("c3:c300").ClearContents
    
        
    
        End With
    
    
    End Sub
    Last edited by nigelog; 05-31-2012 at 04:31 AM.

Thread Information

Users Browsing this Thread

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

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