Range Properties

.VALUE Property
enter $12.50 in cell A1
the message box below will show 12.5
     Sub myValue()
     MsgBox Range("A1").Value
     End Sub
        
Top

Index

.TEXT Property
enter $12.50 in cell A1
the message box below will show $12.50
     Sub myValue()
     MsgBox Range("A1").Text
     End Sub
        
when no property is set the message box will display the Value property of the cell
     Sub myValue()
     MsgBox Range("A1")
     End Sub
        
Top

Index

.ROW and .COLUMN Properties
the first message box displays 6
the second message box displays 5
    Sub myColumnOrRow()
    MsgBox Range("E6").Row
    MsgBox Range("E6").Column
    End Sub
    
Top

Index

.SELECT Property
the macro below selects the named range on the worksheet
    Sub mySelection()
    Range("<range name>").Select
    End Sub
    
Top

Index

.COUNT Property
the macro below counts the number of cells in the range
    Sub myCount()
    MsgBox Range("<range name>").Count
    MsgBox Range("A1:C6").Count
    End Sub
    
Top

Index

.ADDRESS Property
Address function returns the address for a cell based on a given row and column number
    Sub myAddress()
    MsgBox Range("A1").Address(1,1) ' displays $A$1
    MsgBox Range("A1").Address(0,1) ' displays $A1
    MsgBox Range("A1").Address(0,0) ' displays A1
    End Sub
    
Top

Index

.FORMULA Property
the macro below sums the values of the range and writes the total to the desired cell
when cell values change
    Sub myFormula()
    Range("C9").Formula = "= SUM(A1:C5)"
    End Sub
    
Top

Index

.NUMBERFORMAT Property
the macro below changes the number format e.g. 12 becomes 12.00
    Sub myNumberFormat
    Range("A1:C5").NumberFormat = "0.00"
    End Sub
    
Top

Index

FONT.BOLD, UNDERLINE or ITALIC Properties
the macro below shows how to set font properties
    Sub myFonts()
    Range("A1:C5").Font.Bold = true
    Range("A1:C5").Font.Italic = true
    Range("A1:B4").Font.Underline = true
    
Top

Index

Exercise 2a
    Sub exercise2a()
        Range("A1") = "ID"
        Range("A2") = 1
        Range("A3") = 2
        Range("A4") = 3
        Range("B1") = "Name"
        Range("B2") = "Name1"
        Range("B3") = "Name2"
        Range("B4") = "Name3"
        Range("A1,B1").Font.Bold = True
        Range("C1").Select
    End Sub
    
Top

Index

Exercise 2b
    Sub exercise2b()

        Range("A1") = "ID"
        Range("B1") = "Name"
        Range("C1") = "Sales"
        Range("A1:C1").Font.Bold = True

        Range("A2") = 1
        Range("A3") = 2
        Range("A4") = 3

        Range("B2") = "Name1"
        Range("B3") = "Name2"
        Range("B4") = "Name3"

        Range("C2") = 10
        Range("C3") = 13
        Range("C4") = 21

        Range("C4").Select

        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With

        Range("C4").Select
        Range("C5").Formula = "=Sum(C2:C4)"

        Range("C2:C5").NumberFormat = "0.00"

    End Sub
    
Top

Index

n4jvp.com