Salt * Wet * Bytes

March 16, 2007

Excel: GetFilePath()

Filed under: AdminScripts — saltwetfish @ 8:20 am
Tags:

Somethings you need to get the current file path of the active workbook. Here is a function you can use. It returns a folder path ending with “\”, e.g. c:\winnt\system32\.

Public Function GetFilePath()

Dim FullPathFN

FullPathFN = ActiveWorkbook.FullName
GetFilePath = Mid(FullPathFN, 1, InStrRev(FullPathFN, “\”))

End Function

Excel: SheetExists()

Filed under: AdminScripts — saltwetfish @ 8:15 am
Tags:

Excel VBA does not provide a method to checking if a worksheet exists or not. A simple function below provides one such solution:

Public Function SheetExists(ByVal SheetName)

Dim wsTestSheet As Worksheet

On Error Resume Next
Set wsTestSheet = Sheets(SheetName)

If wsTestSheet Is Nothing Then

SheetExists = False

Else

SheetExists = True

End IfSet wsTestSheet = Nothing
On Error GoTo 0

End Function

Blog at WordPress.com.