Adjust VBA to close file after opening

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
797
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,
 
Hi Fluff. you are always so helpful. So the vba is pretty lengthy so i wont copy it all in here, but how it works is

sub process - VBA doing a lot of other stuff
sub opencopy - inserted this sub into sub process
sub process - VBA doing a lot of other stuff

so i guess i could break up the vba so the open copy is the full vba? or could i create an opencopy2 with the above to just close that same file opened by opencopy? so ends up like this

sub process - VBA doing a lot of other stuff
sub opencopy - inserted this sub into sub process
sub opencopy2 - NEW to close file?
sub process - VBA doing a lot of other stuff

i also, came across Workbooks(2).Close
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You can put it all in one sub.
 
Upvote 0
Thanks will work on combining it all together. appreciate the help. thanks fluff have a wonderful weekend
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,999
Messages
6,128,186
Members
449,431
Latest member
Taekwon

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