Excel VBA

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.

It may seem trivial, but even the smallest things have to be handled carefully when programming.

Therefore I have some standard functions, which handle the creation of new worksheets in my VBA projects. So far I never had trouble with them plus they emphazise NOT to use existing worksheets for your coding. Instead it is better to always create new worksheets. This way you have full control over your programm.

If you start coupling Excel files with source code, you start a war that you cannot win. If your source code relies on a certain status in Excel files, it will fail by time and you spend a lot of time handling errors, which can easily be avoided.

However, here the source code for creating a new worksheet safely.

' @Author - Alexander Bolte
' @ChangeDate - 2014-05-31
' @Description - checks, if a worksheet exists under given name in given workbook.
' @Param myBook - an initialized Excel Workbook, which should be searched for given sheet name.
' @Param sheetName - a String holding the sheets name, which should be searched in given workbook.
' @Returns true, if the sheet exists, else false.
Public Function worksheetExists(ByRef myBook As Workbook, ByVal sheetName As String) As Boolean
Dim ret As Boolean
Dim tmpS As Worksheet

On Error GoTo errHandler:
Set tmpS = myBook.Worksheets(sheetName)
ret = True
Set tmpS = Nothing

errHandler:
If Err.Number <> 0 Then
ret = False
Err.Clear
End If

worksheetExists = ret
End Function


' @Author - Alexander Bolte
' @ChangeDate - 2014-05-31
' @Description - creates a new worksheet with given name in given workbook. If a worksheet already exists under given name, it is deleted and replaced with an empty worksheet.
' @Param myBook - an initialized Excel Workbook, which should be searched for given sheet name.
' @Param sheetName - a String holding the sheets name, which should be created in given workbook.
' @Returns a Worksheet object referencing a newly created worksheet in given workbook.
Public Function createWorksheet(ByRef myBook As Workbook, ByVal sheetName As String) As Worksheet
Dim tmpS As Worksheet

' ### delete the worksheet, if already existing ###
If worksheetExists(myBook, sheetName) Then
myBook.Worksheets(sheetName).Delete
End If
' ### add a new worksheet ###
Set tmpS = myBook.Worksheets.Add
tmpS.Name = sheetName
tmpS.Move myBook.Worksheets(1)

Set createWorksheet = tmpS
End Function

If you do not want Excel to display warning Messages, you can switch them off using the DsiplayAlerts property of the Application object.

' Disable all alerts.
Application.DisplayAlerts = False

I am Mr. Forgetful. What was this password again?

Anyway, if you do not know any better, use brute force!

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
27
28
29
30
31
' @Author - Alexander Bolte
' @ChangeDate - 2013-12-15
' @Description - Removes the password from a protected worksheet using brute force.
' Select a protected worksheet and start the sub.
Sub PasswordBreaker()
    Dim i As Integer, j As Integer, k As Integer
    Dim l As Integer, m As Integer, n As Integer
    Dim i1 As Integer, i2 As Integer, i3 As Integer
    Dim i4 As Integer, i5 As Integer, i6 As Integer
    Dim trg As Worksheet
  
    On Error Resume Next
  
    Set trg = ActiveSheet
  
    For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
    For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
    For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
    For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
        trg.Unprotect Chr(i) & Chr(j) & Chr(k) & _
            Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
            Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
        If trg.ProtectContents = False Then
            MsgBox "One usable password is " & Chr(i) & Chr(j) & _
              Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _
              Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n), vbInformation, ";0)"
            Exit Sub
        End If
    Next: Next: Next: Next: Next: Next
    Next: Next: Next: Next: Next: Next
End Sub

Whenever you have a list of unique key values identifying one file in a directory, but the file name contains other text as well it is possible to use wildcards in order to open the file you want.

Below code snippet will do the trick for you.

Source Code

The function Dir() returns an empty String (""), in case a file could not be identified. Therefore you will need extra handling for this case.

In case more than one file was identified by Dir(), only the first file (whichever the first may be) is returned.

1
2
3
4
5
6
Dim sFound As String
 
sFound = Dir(ActiveWorkbook.Path & "\302113*.xlsm")    'the first one found
If sFound <> "" Then
    Workbooks.Open filename:= ActiveWorkbook.Path & "\" & sFound
End If