Close workbook once import is completed

HotNumbers

Well-known Member
Joined
Feb 14, 2005
Messages
732
Can someone please Help me modify a code. I am able to import the sheet that i need, once imported to the open Workbook I need the workbook imported from to close.

Below is my code.

Sub Load_External()
ANS = MsgBox("Import Ledge data from an external workbook source?", vbYesNo + vbQuestion, "Import Ledger Data")
If ANS = vbNo Then Exit Sub

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

'Remove #REF Name Range
Sheets("LEDGER PREP SHEET").Select
Dim nName As Name
For Each nName In Names
If InStr(1, nName.RefersTo, "#REF!") > 0 Then
nName.Delete
End If
Next nName


On Error GoTo Error_Loader

Load:
NAME1 = Application.GetOpenFilename
Set NAME2 = ThisWorkbook

If NAME1 = False Then
ANS = MsgBox("The load process has been canceled. " & vbNewLine & vbNewLine & _
"Click RETRY to restart the load process " & vbNewLine & _
"Click CANCEL to end the load process", vbRetryCancel, _
"Bento Box")
If ANS = vbRetry Then GoTo Load
If ANS = vbCancel Then Exit Sub
End If

Sheets("LEDGER PREP SHEET").Visible = True
Sheets("LEDGER PREP SHEET").Select
Cells.Clear
Range("a1").Select

Workbooks.Open Filename:=NAME1


Set NAME3 = ActiveWorkbook
NM = ActiveWorkbook.Name
Sheets("Ledger Inquiry").Select
Cells.Copy
NAME2.Activate
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False


Beep
MsgBox "Data successfully imported"
Range("a1").Select

Exit Sub

Error_Loader:
MsgBox "Import error"
NAME2.Activate
Sheets("LEDGER PREP SHEET").Visible = False
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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