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
Bookmarks