ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Open a semi variable name with a macro (https://www.excelbanter.com/excel-programming/350217-open-semi-variable-name-macro.html)

Don Seegers

Open a semi variable name with a macro
 
I want to open a semi variable file name with a macro, the 1st characters are
variable the last ones are not, this is the form of the filename : "20060111
- cf_011106_110818_cgcflh.CSV"

everything but cgcflh is variable but is always in the same format,

can anyone help me ??

--
Don Seegers

Dave Peterson

Open a semi variable name with a macro
 
I would let the user select the file:

option explicit
sub testme()
dim myFileName as variant
dim newwkbk as workbook

myfilename = application.getopenfilename("CSV files, *.csv")
if myfilename = false then
exit sub
end if

set curwks = activesheet

set newwkbk = workbooks.open(filename:=myfilename)

'...do some real work

end sub

Don Seegers wrote:

I want to open a semi variable file name with a macro, the 1st characters are
variable the last ones are not, this is the form of the filename : "20060111
- cf_011106_110818_cgcflh.CSV"

everything but cgcflh is variable but is always in the same format,

can anyone help me ??

--
Don Seegers


--

Dave Peterson

Don Seegers

Open a semi variable name with a macro
 
That is what i'm doing now, but users can make mistakes so i want to put in
in the macro.

but thanx anyways

Don Seegers wrote:
I want to open a semi variable file name with a macro, the 1st characters are
variable the last ones are not, this is the form of the filename : "20060111
- cf_011106_110818_cgcflh.CSV"

everything but cgcflh is variable but is always in the same format,

can anyone help me ??


--
Don Seegers

Dave Peterson

Open a semi variable name with a macro
 
If there's only one file like that in the folder:

Option Explicit
Sub testme01()

Dim myFile As String
Dim myPath As String
Dim FoundIt As Boolean
Dim wkbk As Workbook

'change to point at the folder to check
myPath = "c:\my documents\excel"
If Right(myPath, 1) < "\" Then
myPath = myPath & "\"
End If

myFile = ""
On Error Resume Next
myFile = Dir(myPath & "*.csv")
On Error GoTo 0
If myFile = "" Then
MsgBox "no files found"
Exit Sub
End If

FoundIt = False
Do While myFile < ""
If LCase(myFile) Like "*cgcflh.csv" Then
FoundIt = True
Exit Do
End If
myFile = Dir()
Loop

If FoundIt = True Then
Set wkbk = Workbooks.Open(Filename:=myPath & myFile)
End If

End Sub

Don Seegers wrote:

That is what i'm doing now, but users can make mistakes so i want to put in
in the macro.

but thanx anyways

Don Seegers wrote:
I want to open a semi variable file name with a macro, the 1st characters are
variable the last ones are not, this is the form of the filename : "20060111
- cf_011106_110818_cgcflh.CSV"

everything but cgcflh is variable but is always in the same format,

can anyone help me ??


--
Don Seegers


--

Dave Peterson

Don Seegers via OfficeKB.com

Open a semi variable name with a macro
 
Thanx Dave,

This did the trick


Dave Peterson wrote:
If there's only one file like that in the folder:

Option Explicit
Sub testme01()

Dim myFile As String
Dim myPath As String
Dim FoundIt As Boolean
Dim wkbk As Workbook

'change to point at the folder to check
myPath = "c:\my documents\excel"
If Right(myPath, 1) < "\" Then
myPath = myPath & "\"
End If

myFile = ""
On Error Resume Next
myFile = Dir(myPath & "*.csv")
On Error GoTo 0
If myFile = "" Then
MsgBox "no files found"
Exit Sub
End If

FoundIt = False
Do While myFile < ""
If LCase(myFile) Like "*cgcflh.csv" Then
FoundIt = True
Exit Do
End If
myFile = Dir()
Loop

If FoundIt = True Then
Set wkbk = Workbooks.Open(Filename:=myPath & myFile)
End If

End Sub

That is what i'm doing now, but users can make mistakes so i want to put in
in the macro.

[quoted text clipped - 11 lines]
--
Don Seegers



--
Don Seegers

Message posted via http://www.officekb.com


All times are GMT +1. The time now is 05:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com