Adjust VBA to close file after opening

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello,
I have a VBA that will open a file based off of only certain criteria being met. the only ask or modification i'm looking for is how do I leverage the existing VBA to just close the file when its done.

Sub OpenCopy()
Dim sPath As String
Dim sPartial As String
Dim sFName As String

sPath = "C:\" ' <<<<< change accordingly

sPartial = "AAA_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.txt"
sFName = Dir(sPath & sPartial)
If Len(sFName) > 0 Then
Workbooks.OpenText sPath & sFName
Else
MsgBox "File not found.", vbExclamation
End If
End Sub

Thanks,
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
.
Workbook.Close method (Excel)


VBA Code:
Else
MsgBox "File not found.", vbExclamation
End If

Workbooks("YourWorkbookName").Close SaveChanges:=False  '<-- can change to True if you want changes saved

End Sub

So the problem is the workbook name isnt a constant value it changes so that is why my VBA opens it based off a partial file meeting a criteria.
 
Upvote 0
If the other workbook is open at the same time, it should still be viewable after closing the activeworkbook.
 
Upvote 0
If the other workbook is open at the same time, it should still be viewable after closing the activeworkbook.
Yes so that is my dilemma how do i call back that workbook in my vba when i open it off a partial file being met. if i do activeworkbook it will close the workbook i'm using to paste the data in.
 
Upvote 0
.
Your original question was " how do I leverage the existing VBA to just close the file when its done "

The answer has been provided.

If you have other questions not pertaining to closing a workbook, please start another thread.
 
Upvote 0
.
Your original question was " how do I leverage the existing VBA to just close the file when its done "

The answer has been provided.

If you have other questions not pertaining to closing a workbook, please start another thread.
I am sorry i dont follow. my original post is how do i leverage my vba that calls on a partial name to close the workbook. i will start another thread then and clarify the original ask better
 
Upvote 0
How about
VBA Code:
Sub OpenCopy()
Dim sPath As String
Dim sPartial As String
Dim sFName As String
Dim Wbk As Workbook

sPath = "C:\" ' <<<<< change accordingly

sPartial = "AAA_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.txt"
sFName = Dir(sPath & sPartial)
If Len(sFName) > 0 Then
   Workbooks.OpenText sPath & sFName
   Set Wbk = ActiveWorkbook
Else
   MsgBox "File not found.", vbExclamation
End If
'do something
Wbk.Close
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,168
Messages
6,123,402
Members
449,098
Latest member
ArturS75

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