|
-
Jul 8th, 2015, 02:19 PM
#1
Thread Starter
New Member
Truncated Extension-free Filename
Hello everyone,
Since this is my first post I must preface with some relevant information. I have some (minimal experience) with VBA, but I would like to become more fluent. Currently, I am working on a project that requires some manipulation of a previously generated macro. The function of the macro is to take temporary output files, extract the data and import into excel for analysis. The software that generates the output files had some minor changes made when it was upgraded from an older version and I am stuck on one issue-implementing the truncated and extension-free filename into my spreadsheet. Ideally, the solution should be compatible across older and newer versions of excel (v.8 - v.15), if possible.
My desired outcume is to:
- Extract Filename = C:\path\....\SampleX.out
- Truncate Filename = SampleX.out
- Remove File Extension = "SampleX"
- Insert Filename to spec. Cells = Cells(1, 2)
- "Cells(1, 2)" Contents to "Cells(myrow, 1)
Here is some of the relevant code that I have so far:
Private Sub CmdButton_Click()
Dim SampleX As String
Filename$ = Application.GetOpenFilename("[ApplicationName]Output file(*.*), *.*")
If Len(Filename$) <= 0 Then Exit Sub
If Filename$ = "False" Then Exit Sub
Filename$ = Cells(1, 5)
Open Filename$ For Input As #1
Line Input #1, strtemp$
Do While (Not EOF(1))
s$ = Right(strtemp$, 5)
If s$ = "*****" Then
Line Input #1, SampleX
......
Exit Do
End If
Line Input #1, strtemp$
Loop
Close #1
aaa% = InStrRev(Filename$, "\")
s$ = Right(Filename$, Len(Filename$) - aaa%)
s$ = Left(s$, Len(s$) - 4)
myrow = 31
Do While (Cells(myrow, 1) <> "")
myrow = myrow + 1
Loop
Cells(1, 2) = s$
Cells(myrow, 1) = SampleX
....
End Sub
Solutions and detailed explanations are highly appreciated!
Thanks,
Mr. T. Ferguson
-
Jul 8th, 2015, 10:52 PM
#2
Re: Truncated Extension-free Filename
This could be an issue
Code:
s$ = Left(s$, Len(s$) - 4)
It is possible that a file may have more or less than 3 characters as an extension or even no extension at all so just blindly trimming off the last 4 characters may not be a good idea.
Would be better to check for the . and if it exists trim it and what follows.
This really belongs in the Office section since it seems to be Excel VBA rather than VB
-
Jul 9th, 2015, 06:54 PM
#3
Re: Truncated Extension-free Filename
And to piggy back on DataMiser's suggestion. Look for the last backslash first then the 1st decimal (.) after that, should you have a path like the following with no extension on the file name...
C:\Temp\2014.15.25\mySampleLog
-
Jul 9th, 2015, 07:31 PM
#4
Re: Truncated Extension-free Filename
To be fair, to help, we'd need to know what you're getting vs what you're expecting.
and I think lavolpe meant the last period, not the first...
-tg
-
Jul 9th, 2015, 08:09 PM
#5
Re: Truncated Extension-free Filename
 Originally Posted by techgnome
Tand I think lavolpe meant the last period, not the first...
-tg
-
Jul 10th, 2015, 07:38 AM
#6
Re: Truncated Extension-free Filename
Here is some of the relevant code that I have so far:
while some improvements could be made to the code, it looks like it should work
what problems are you having with it?
are you getting errors or wrong results?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 13th, 2015, 10:37 PM
#7
Thread Starter
New Member
Re: Truncated Extension-free Filename
 Originally Posted by DataMiser
This could be an issue
Code:
s$ = Left(s$, Len(s$) - 4)
Omitted this line.
 Originally Posted by techgnome
To be fair, to help, we'd need to know what you're getting vs what you're expecting.
The code is having me manually select the file that I would like to have automatically selected. I'm expecting that the code will automatically choose the the most recently modified *.out file in the specified path.
 Originally Posted by westconn1
while some improvements could be made to the code, it looks like it should work
what problems are you having with it?
are you getting errors or wrong results?
Most of the errors have been corrected. File name is going to the right place now in the array. The only issue now is that if I try to autoselect the most recently modified output file, I get the wrong values in my spreadsheet. Guessing this is the result of using the wrong function as there are several different file formats in the spec'd folder (same file name--different extension).
Sorry for the delayed response.
TF
-
Jul 14th, 2015, 05:14 AM
#8
Re: Truncated Extension-free Filename
Filename$ = Application.GetOpenFilename("[ApplicationName]Output file(*.*), *.*")
change this to the specific extension required
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|