Adjust VBA to close previously active workbook

Status
Not open for further replies.

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
733
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2010
  5. 2007
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 close an active workbook based off the the partial name as called out below? the VBA will open a file based off the below, copy the data in said file and paste it into another active workbook. the issue is the other workbook (vba below) remains open. And having trouble calling that active workbook back seeing the file name changes everyday and is originally being open by a partial parameter below.

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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Disregard my post #2. The wording of your question is confusing as it includes reference to your original question in the other post. For clarity it would be best to specify your single goal
rather than include both goals.
 
Upvote 0
Right so maybe I dont know how to word it. That is why I tried to give as much information. I have a workbook being opened off of partial parameters being met. When I have multiple workbooks open how do I call back that workbook to close it. If I do activeworkbook.close it tries to close the workbook i am working on. and the workbook name will change everyday that is why its originally being opened off of partial parameters being met. I hope that better clarifies it
 
Upvote 0
in theory if its always the 2nd workbook i have open i guess i could use:

Workbooks(2).Close Savechanges:=False
 
Upvote 0
As this is a duplicate I will close it & you need to continue in your previous thread.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,213,531
Messages
6,114,167
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