Code Snippets
Access it using the worksheet.ListIbjects property
Dim mytable As ListObject
mytable.DataBodyRange - the cells containing the actual data (excluding header & insert row)
mytable.HeaderRowRange
mytable.ShowAutoFilter
mytable.ShowTables
mytables.TotalsRowRange
Usually the simplest way to extract data from a database is by means of the MS Query program.
This can be launched from (Data > Get External Data > New Database Query).
The settings implemented in MS Query can be controlled by the QueryTable object.
In Excel 2000 this object has a lot more properties.
Display the data form
Before you can display the Data Form you must select a cell in the database table first.
Range("B2").Select
Application.ActiveSheet.ShowDataForm
ActiveSheet.ShowDataForm
There are several ways to connect to an external data source
Before you can connect to a database, you must add the appropriate reference to your project.
Languages
Tables
English Arial paried with MS Gothic
English Expert Sans Regular paired with MS Mincho
Ideally want Expert Sans Regular with MS Gothic
Sub cMethodDAO()
Dim strDBFullName As String
Dim dbData As Database, rstWork As Recordset, strSQL As String
strDBFullName = ThisWorkbook.Path & "\" & ThisWorkbook.Name
strSQL = "select distinct [your_field] from dataarea"
'Appropriate driver needed for this statement
Set dbData = OpenDatabase(strDBFullName, False, True, _
Excel8.0;HDR=YES;)
Set rstWork = dbData.OpenRecordset(strSQL)
rstWork.MoveLast
MsgBox rstWork.RecordCount
Set rstWork = Nothing
Set dbData = Nothing
End Sub
where [your_field] is the header of the column you are interested in and
the dataarea is a named area that contains all data in question (could be
the single column you are interested in).
Sub CountUniqueByPivotTable()
On Error GoTo uOut
Application.ScreenUpdating = False
Application.DisplayAlerts = False
TheHeader = ActiveCell.Value
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, _
SourceData:=ActiveSheet.Name & "!" & _
Selection.Address, TableDestination:="", TableName:="uPivotTable"
ActiveSheet.PivotTables("uPivotTable").AddFields RowFields:=TheHeader
ActiveSheet.PivotTables("uPivotTable").PivotFields(TheHeader). _
Orientation = xlDataField
MsgBox Application.WorksheetFunction.CountA(Range("a:a")) - 3
ActiveSheet.Delete
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Exit Sub
uOut:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Although not tested extensively, it appears that the procedure that uses the Collection object produces the fastest result.
Sub cMethodByCollection()
CountUniqueByCollection Selection.Address
End Sub
Sub CountUniqueByCollection(AllCells As String)
Dim NoDupes As New Collection
On Error Resume Next
For Each Cell In Range(AllCells)
NoDupes.Add Cell.Value, CStr(Cell.Value)
'Note: the 2nd argument (key) for the Add method must be a string
Next Cell
On Error GoTo 0
End Sub
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext