ItalianPlatinum
Well-known Member
- Joined
- Mar 23, 2017
- Messages
- 733
- Office Version
- 2021
- 2019
- 2016
- 2010
- 2007
- Platform
- 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,
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,