If you need to know a files name from a file path it might be interesting to also know, if this file is alos existing.
Below you will find a method, which only returns a files name if the file is available in current file system.
Source Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
' @Author - Alexander Bolte' @ChangeDate - 2014-10-21' @Description - Returning a files name from a given file path.' @Param filePath - a String providing a full path to one file.' @Returns - a String holding a files name, if file exists and is accessible. Else an empty String is returned.PublicFunctiongetFileNameFromFilePath(ByValfilePathAsString)AsStringDimfileNameAsStringDimfsoAsNewscripting.FileSystemObjectDimfAsscripting.FileOnErrorGoToerrHandle:Iffso.fileExists(filePath)ThenSetf=fso.GetFile(filePath)fileName=f.NameEndIfSetf=NothingSetfso=NothingerrHandle:IfErr.Number<>0ThenErr.ClearEndIfgetFileNameFromFilePath=fileNameEndFunction
If you have to delete a worksheet from a workbook, you can use below source code.
Source Code
1
2
3
4
5
6
7
8
9
10
' @Author - Alexander Bolte' @ChangeDate - 2014-10-08' @Description - Deleting a worksheet named like sheetName from given target Workbook, if a worksheet under given sheetName exists.' @Param trg - an initialized workbook object.' @Param sheetName - a String holding a unique worksheet name.PublicFunctiondeleteWorksheet(ByReftrgAsWorkbook,ByValsheetNameAsString)IfworksheetExists(trg,sheetName)ThenCalltrg.Worksheets(sheetName).DeleteEndIfEndFunction