VBA - Visual Basic for Applications

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.
Public Function deleteWorksheet(ByRef trg As Workbook, ByVal sheetName As String)
    If worksheetExists(trg, sheetName) Then
        Call trg.Worksheets(sheetName).Delete
    End If
End Function

Referenced Functions

The following functions are referenced.

 Check if a worksheet exists

 

In case you need the selected list item from a listbox, which only allows single selection the selected list item can be returned using the below source code.

Source Code

1
Me.listBox.column(0)

Where listBox is the name of the control element / the listbox as you defined it.

Me refers to a form a listbox is placed in.

The column index within a listbox is zero based.

The following method returns a Range object referneced by a NamedRange in an Excel worksheet. Motivation for this article is the fact that VBA does not handle possible errors in a NamedRange. If a referenced range contains errors even in formulas, the simple calll to RefersToRange of a NamedRange will fail.

Subcategories

This category will hold articles regarding developement in Excel VBA. It will serve as a wiki and an Excel VBA Framework for myself.

Some development tasks reoccur for every customer. Since I am a lazy bum it will be nice to have a central source where I can reuse source code from.

This category holds articles regarding general things in MS Office VBA independent from the MS Office application.  

This category holds articles regarding Access VBA, but also general things I come accross Access and its usage in companies.