raison d'etre

I have often found bits and snippets of code on the web that I have incorporated into things I'm working on. I take no responsibility for these code samples. Most of them have been laboriously gleaned off of the web, but they're real handy to have quick access to. So bookmark this and enjoy...

JovialJohn

Tuesday, May 4, 2010

Some Useful Functions

The FileExists Function

Private Function FileExists(fname) As Boolean
' Returns TRUE if the file exists
Dim x As String
x = Dir(fname)
If x <> "" Then FileExists = True _
Else FileExists = False
End Function

The FileNameOnly Function
Private Function FileNameOnly(pname) As String
' Returns the filename from a path/filename string
Dim i As Integer, length As Integer, temp As String length = Len(pname) temp = "" For i = length To 1 Step -1 If Mid(pname, i, 1) = Application.PathSeparator Then FileNameOnly = temp Exit Function End If temp = Mid(pname, i, 1) & temp Next i FileNameOnly = pname End Function

The PathExists Function
Private Function PathExists(pname) As Boolean
' Returns TRUE if the path exists
Dim x As String On Error Resume Next x = GetAttr(pname) And 0 If Err = 0 Then PathExists = True Else PathExists = False End Function

The RangeNameExists Function
Private Function RangeNameExists(nname) As Boolean
' Returns TRUE if the range name exists
Dim n As Name RangeNameExists = False For Each n In ActiveWorkbook.Names If UCase(n.Name) = UCase(nname) Then RangeNameExists = True Exit Function End If Next n End Function

The SheetExists Function
Private Function SheetExists(sname) As Boolean
' Returns TRUE if sheet exists in the active workbook
Dim x As Object On Error Resume Next Set x = ActiveWorkbook.Sheets(sname) If Err = 0 Then SheetExists = True Else SheetExists = False End Function

No comments:

Post a Comment