Private Const scModuleName As String = "Module3."
Sub CreateTable()
'---------------------------------------------------------------------------------------
' Procedure : CreateTable
' Author : shell_l_d
' Date : 10Jul2010
' Purpose : Create table at A2 on selected worksheet (tablestyle,totalrow,freezepanes,autofit)
'---------------------------------------------------------------------------------------
' For Error Reporting
Dim sErrorDescr As String
Const sErrSource As String = scModuleName & "CreateTable"
1 On Error GoTo Error_In_CreateTable
Dim iLastCol As Integer, iLastRow As Integer, iPos As Integer, iMaxWidth As Integer
Dim sEndTable As String, sLastColRef As String, sTblName As String
Dim oRngCol As Object
2 iMaxWidth = 20
3 If ActiveSheet.Name = "Update" Then
4 GoTo Exit_CreateTable
5 ElseIf ActiveSheet.Name = "VbReferences" Or ActiveSheet.Name = "TimeZones" Then
6 iMaxWidth = 0
7 End If
8 With ActiveSheet
9 .Range("A2").Select
10 sTblName = "tbl" & .Name
' Set Font type & size for worksheet
11 .Cells.Font.Size = 10
' find position of entire table including headings
12 iLastCol = .Range("A1").End(xlToRight).Column
13 iLastRow = 1 ' default in case no data
' Ignore possible Error '6 Overflow' if only header exists (no data)
14 On Error Resume Next
15 iLastRow = .Range("A1").End(xlDown).Row
16 On Error GoTo Error_In_CreateTable
' extract the column letter from sEndTable eg: AB from "$AB$100"
17 sEndTable = .Cells(iLastRow, iLastCol).Address
18 iPos = VBA.InStrRev(sEndTable, "$", -1)
19 sLastColRef = VBA.Mid(sEndTable, 2, iPos - 2)
' Add table - ignore error if table already exists
20 On Error Resume Next
21 .ListObjects.Add(xlSrcRange, Range("$A$1:" & sEndTable), , xlYes).Name = sTblName
22 On Error GoTo Error_In_CreateTable
' Tablestyle for new table (adds filtering, colours & totals)
23 With .ListObjects(sTblName)
24 .TableStyle = "TableStyleMedium9" ' blue
25 .ShowHeaders = True
26 .ShowTotals = True
27 End With
' iRow height
28 .Rows("1:1").RowHeight = 40 'headings
29 .Rows("2:" & iLastRow).RowHeight = 15 'data
' Column width & max width
30 .Cells.EntireColumn.AutoFit
31 With Columns("A:" & sLastColRef)
32 For Each oRngCol In .Columns
33 If iMaxWidth > 0 And oRngCol.ColumnWidth > iMaxWidth Then
34 oRngCol.ColumnWidth = iMaxWidth
35 End If
36 Next
37 End With
' Format Headings
38 Range(sTblName & "[#Headers]").Select
39 With Selection
40 .HorizontalAlignment = xlCenter
41 .VerticalAlignment = xlCenter
42 .WrapText = True
43 End With
' Freeze panes below headings
44 ActiveWindow.FreezePanes = False
45 Range("C2").Select
46 ActiveWindow.FreezePanes = True
47 End With
' ===== Exit Handler =====
Exit_CreateTable:
48 On Error Resume Next
' Release memory used by Objects
49 If Not oRngCol Is Nothing Then oRngCol = Nothing
50 Exit Sub
' ===== ERROR HANDLER =====
Error_In_CreateTable:
51 With Err
52 sErrorDescr = "Error '" & .Number & " " & _
.Description & "' occurred in " & sErrSource & _
IIf(Erl <> 0, " at line " & CStr(Erl) & ".", ".")
53 End With
54 Select Case MsgBox(sErrorDescr, vbAbortRetryIgnore, "Error in " & sErrSource)
Case vbRetry
55 Resume
56 Case vbIgnore
57 Resume Next
58 Case Else
59 Resume Exit_CreateTable
60 End
61 End Select
End Sub