View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default How to trim cell information

For a UDF, which allows for file types of three or four characters, you
could
try this:

===============================
Option Explicit
Function reFilename(str As String) As String
Dim re As Object
Dim mc As Object
Const sPat As String = "\\([^\\\,]+)\..{3,4}$"

Set re = CreateObject("vbscript.regexp")
re.Pattern = sPat
If re.test(str) = True Then
Set mc = re.Execute(str)
reFilename = mc(0).submatches(0)
End If
End Function
==================================


Here is a UDF which works for any extension (even for a file with no
extension)...

Function GetFileName(FN As String) As String
If Len(FN) Then GetFileName = Split(Split(FN, "\") _
(UBound(Split(FN, "\"))), ".")(0)
End Function

Rick


That'll work, and is shorter and probably faster.


I don't really have a feel for the speed difference myself. Split is not the
fastest function in the VB arsenol, but the of course, neither are calls to
scripting engines. The String values either of our functions will have to
deal are quite small, so any Split/Scripting speed differences may well be
nil. It is even possible that the VBA-to-spreadsheet interface time penalty
may be such as to make the Split and/or Scripting speeds appear negligiable.

I could modify mine to handle the absence of a file type extension.


I think you should do so. That way the OP (or others following the thread)
will have a choice. Besides, if the OP (or others readers) come from a
Regular Expression background, they will more than likely find your solution
far more appealing than mine.

Rick