VBA - Visual Basic for Applications

If you have to write an ado db recordset into an Excel worksheet, you have two basic options.

Either you write a recordset object using Excel API function copyFromRecordset or you write the data implementing an own method looping through all records.

However calling the function copyFromRecordset is pretty fast compared to looping through all records, it opposes the risk of incorrectly formatted data in a target worksheet. Root cause for this is the often failing attempt of ADO to guess a data type of a column in a source worksheet based on the first few records.

You can switch some optional parameter in a connection string called IMAX in order to have ADO take all records of a field in a recordset into account before guessing a field type, but this will lead only to incredible bad performance.

On top of that ADO is still likely to fail in guessing a field type of an Excel column even, if you use IMAX.

Recently a collegue had to import a SharePoint list into Excel for automated processing.

Below you can find the source code that I provided him with as an example.

Source Code

1
2
3
4
5
6
7
8
9
Sub LinkedSharePointList()
 
    ActiveSheet.ListObjects.Add SourceType:=xlSrcExternal, _
 
        Source:=Array(<"URL String">, <"ListNameString">, _
 
        <"{GUIDString}">), LinkSource:=True, Destination:=Range("A1")
 
End Sub

Actually you can list all worksheet names in a workbook using the object path Workbook.Worksheets(index).Name.

I needed to have the names as strings in a VBA.Collection, so I developed a little function collecting the names.

' @Author - Alexander Bolte
' @Change Date - 26.12.2013
' @Description - Creates a list of all sheet names in given workbook.
' @Param wrk - Excel workbook the sheet name collection should be created from.
' @Returns - VBA.Collection holding all sheet names from given Microsoft Excel workbook.
Public Function getSheetNamesFromWorkbook(ByRef wrk As Workbook) As VBA.Collection
Dim i As Integer
Dim sheetNames As New VBA.Collection

On Error GoTo errHandle:

For i = 1 To wrk.Worksheets.Count
sheetNames.Add wrk.Worksheets(i).Name
Next i

errHandle:
If Err.Number <> 0 Then
Err.Clear
End If

Set getSheetNamesFromWorkbook = sheetNames
End Function

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.