How to close a workbook in VBA after opening it, without saving any changes?

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I have some code which opens a file and imports data from it.

However, once data has been imported from the file that has been opened, I would like to close the file (that the data is imported from).

Does anyone know how to adjust this code so that it closes the iWB file without saving changes to it, please? (The iWB file is the file that data is imported from).

VBA Code:
Sub CloseTab1()


Dim fd As FileDialog
Dim filewaschosen As Boolean
Dim Report As Workbook
Dim iWB As Workbook


Set Report = ActiveWorkbook
Set fd = Application.FileDialog(msoFileDialogOpen)
fd.Filters.Clear
fd.Filters.Add "Custom Excel Files", "*.xlsx, *.xlsm, *.xls"
fd.AllowMultiSelect = False
fd.InitialFileName = Environ("UserProfile") & "\Box\"

If fd.Show <> -1 Then
   MsgBox "You didn't select a file?"
   Exit Sub
End If
fd.Execute



lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Range("A2:G" & lastrow).Copy
Report.Activate
ACV.Visible = True
ACV.Activate
Range("A2:G" & lastrow).PasteSpecial

  ControlCenter.Activate
    Range("J9").Activate
    
    
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
How about
VBA Code:
If fd.Show <> -1 Then
   MsgBox "You didn't select a file?"
   Exit Sub
End If
Set iWB = Workbooks.Open(fd.SelectedItems(1))



LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("A2:G" & LastRow).Copy
Report.Activate
ACV.Visible = True
ACV.Activate
Range("A2:G" & LastRow).PasteSpecial

  ControlCenter.Activate
    Range("J9").Activate
iWB.Close False
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,391
Members
448,957
Latest member
Hat4Life

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