  # Free Excel VBA Tutorial

This is a free excel vba tutorial to create a multiplication table. If you were searching for a beginner's tutorial, please see our
free VBA tutorials to learn the basics and your way around the VBA Editor.

To place the following code in a module and then run it...

1. Open a new Excel workbook. (The code that follows creates a multiplication table whose first/lowest numerator appears in cell "A1" so type in a number in cell A1. You can enter the number 1 in the cell and then press Enter or an arrow key to take Excel out of the Edit Mode or the code window won't let you work in it)

2. Go to Tools menu, click Macro, and then Visual Basic Editor (or press ALT + F11).

3. in the VBA Project pane at the upper left below the tool bars, double click on "ThisWorkbook" to open the code area for this workbook, which should appear as the major part of the screen and it should be blank.

4. Copy and Paste the following code in the code window.

5. To run the code, click somewhere in between the first and last lines inclusively and press F5.

### Code for your Free Excel VBA Tutorial

Sub MultiplicationTable()
If Val(Range("A1")) < 1 Then
Range("A1") = "1"
Exit Sub
End If
Range("A3") = Range("A1")
Range("A4") = Range("A1") + 1
Range("A5") = Range("A1") + 2
Range("A6") = Range("A1") + 3
Range("A7") = Range("A1") + 4
Range("A8") = Range("A1") + 5
Range("A9") = Range("A1") + 6
Range("A10") = Range("A1") + 7
Range("A11") = Range("A1") + 8
Range("A12") = Range("A1") + 9
Range("B2") = Range("A1")
Range("C2") = Range("A1") + 1
Range("D2") = Range("A1") + 2
Range("E2") = Range("A1") + 3
Range("F2") = Range("A1") + 4
Range("G2") = Range("A1") + 5
Range("H2") = Range("A1") + 6
Range("I2") = Range("A1") + 7
Range("J2") = Range("A1") + 8
Range("K2") = Range("A1") + 9
' Lines that start with an apostrophe tell VBA that this line is a comment.
' The next ten lines fill in the first line of multiplied products.
Range("B3") = "=R2C2*RC[-1]"
Range("C3") = "=R2C3*RC[-2]"
Range("D3") = "=R2C4*RC[-3]"
Range("E3") = "=R2C5*RC[-4]"
Range("F3") = "=R2C6*RC[-5]"
Range("G3") = "=R2C7*RC[-6]"
Range("H3") = "=R2C8*RC[-7]"
Range("I3") = "=R2C9*RC[-8]"
Range("J3") = "=R2C10*RC[-9]"
Range("K3") = "=R2C11*RC[-10]"
Range("B3:K3").Select
' The next line automatically fills the other 9 lines using Excel's AutoFill feature.
Selection.AutoFill Destination:=Range("B3:K12")
Range("B4").Select
End Sub

' End of code for the free excel vba tutorial.

Site Build It homepage

.

Return from Free Excel VBA Tutorial to VBA Code Samples