And to let the user specify the dimensions he wants use sth like in this file: CreateTable.xlsm
contains:
Option Explicit
Sub CreateTable()
Const DEFAULT_ROW As Long = 12
Const DEFAULT_COL As Long = 12
Dim vntInput1, vntInput2
On Error GoTo CreateTable_ErrorHandler
Application.ScreenUpdating = False
'get number of rows:
Do
vntInput1 = Application.InputBox("Enter the number of Rows.", "Create Table", DEFAULT_ROW, , , , , 1)
If TypeName(vntInput1) = "Boolean" Then
Exit Do
ElseIf CLng(vntInput1) <> vntInput1 Then
Select Case MsgBox("A Number without decimals is required, do you want to re-enter or use the default value of " & _
DEFAULT_ROW & "?" & String(2, vbLf) & "OK: Re-Enter" & vbLf & "No: Use default " & _
DEFAULT_ROW & vbLf & "Cancel: Exit program", vbQuestion + vbYesNoCancel, "Create Table")
Case vbNo
vntInput1 = DEFAULT_ROW
Case vbCancel
GoTo CreateTable_Proc_Exit
End Select
Else
Exit Do
End If
Loop
'get number of cols:
Do
vntInput2 = Application.InputBox("Enter the number of Columns.", "Create Table", DEFAULT_COL, , , , , 1)
If TypeName(vntInput2) = "Boolean" Then
Exit Do
ElseIf CLng(vntInput2) <> vntInput2 Then
Select Case MsgBox("A Number without decimals is required, do you want to re-enter or use the default value of " & _
DEFAULT_COL & "?" & String(2, vbLf) & "OK: Re-Enter" & vbLf & "No: Use default " & _
DEFAULT_COL & vbLf & "Cancel: Exit program", vbQuestion + vbYesNoCancel, "Create Table")
Case vbNo
vntInput2 = DEFAULT_COL
Case vbCancel
GoTo CreateTable_Proc_Exit
End Select
Else
Exit Do
End If
Loop
'make table:
ActiveSheet.Range("A1").Resize(CLng(vntInput1), CLng(vntInput2)).Value = "x"
CreateTable_Proc_Exit:
Application.ScreenUpdating = True
Exit Sub
CreateTable_ErrorHandler:
MsgBox "Error: " & Err.Number & " (" & Err.Description & ") in Sub 'CreateTable' of Module 'Module1'.", vbOKOnly + vbCritical, "Error"
Resume CreateTable_Proc_Exit
End Sub
Bookmarks