How to Close A Workbook without saving it

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
112
Office Version
  1. 2019
Platform
  1. Windows
Hello there. Good day, I'm having problem running my code wherein I have to copy some data from another workbook and after copying needed data, I just want to close it immediately without saving.
Here's my code:

VBA Code:
Sub Test1()
    Application.ScreenUpdating = False
    Dim lastrow As Long, srcWB As Workbook, desWS As Worksheet

    Dim MyLoc As String, MyFile As String
    Dim wb As Workbook

    MyLoc = "C:\Users\ADMIN\Desktop\Samples\" & ThisWorkbook.Sheets("data").Range("A3").Value & "\AllProcess\Process1\"
    MyFile = MyLoc & ThisWorkbook.Sheets("data").Range("B5").Value
    
    Set wb = Workbooks.Open(MyFile)
        
    Set srcWB = wb
    Set desWS = ThisWorkbook.Sheets("Process1")
    With srcWB
        With .Sheets("SEMI")
            lastrow = .Range("E" & .Rows.Count).End(xlUp).Row
            .Range("A2:A" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "D").End(xlUp).Offset(1).PasteSpecial xlPasteValues
    
    
    Workbooks(MyFile).Close SaveChanges:=False
        
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
HTH. Dave
Code:
MyLoc = "C:\Users\ADMIN\Desktop\Samples\" & ThisWorkbook.Sheets("data").Range("A3").Value & "\AllProcess\Process1\"
    MyFile = MyLoc & ThisWorkbook.Sheets("data").Range("B5").Value
    
    Set Wb = Workbooks.Open(MyFile)
        
    'Set srcWB = wb
    Set desWS = ThisWorkbook.Sheets("Process1")
    With Wb
        With .Sheets("SEMI")
            lastrow = .Range("E" & .Rows.Count).End(xlUp).Row
            .Range("A2:A" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "D").End(xlUp).Offset(1).PasteSpecial xlPasteValues
    
        End With
    Application.CutCopyMode = False
    Application.DisplayAlerts = False
    .Close SaveChanges:=False
    End With
 
Upvote 0
Try this.
VBA Code:
Sub Test1()
Dim srcWB As Workbook
Dim desWS As Worksheet
Dim lastrow As Long
Dim MyLoc As String, MyFile As String

    Application.ScreenUpdating = False

    MyLoc = "C:\Users\ADMIN\Desktop\Samples\" & ThisWorkbook.Sheets("data").Range("A3").Value & "\AllProcess\Process1\"
    MyFile = MyLoc & ThisWorkbook.Sheets("data").Range("B5").Value
    
    Set srcWB = Workbooks.Open(MyFile)
    Set desWS = ThisWorkbook.Sheets("Process1")
    
    With srcWB
        With .Sheets("SEMI")
            lastrow = .Range("E" & .Rows.Count).End(xlUp).Row
            .Range("A2:A" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "D").End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End With
        .Close SaveChanges:=False
    End With
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,205
Members
448,554
Latest member
Gleisner2

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