VBA RANGE Property

     

     

    Range Property:

     

  • In expression Cell 1 is mandatory., where as Cells 2 afterwards it is optional
  •  

  • It returns only RANGE, which is an object
  •  

  • It returns a RANGE Object., which consists of ONE or MORE cells
  •  

     

    Private Sub CommandButton1_Click()
    'Single cell as range
    Sheets("Sheet3").Range("A5").Value = 15
    'Multi Cells as Range - more than one cell
    Sheets("Sheet3").Range("D14:F25").Value = 25
    End Sub

     

    Combining of Multiple Ranges:

     

    Sub Numbers_print()
    Worksheets("Sheet3").Range("A1:C9, D4:F15, G1:M5").Value = 55
    End Sub

     

    Range in Brackets

     

  • We need no mention cell address in Quotes
  • Instead of range("A15") we can mention [A15]
  •  

    Sub range_in_brackets()
    Range("A15").Select
    [b5].Select
    Sheets("Sheet2").[c15].Select
    [A5:B15].Select
    'Multiple ranges
    [c5,f10,g15].Select
    End Sub


     

    Range Min & Max & Number of cells

     

     

     

    Named Ranges

     

    Sub Rangenames()
    'Name the ranges as abc,def,aaa
    Range("abc,def,aaa").Select
    End Sub

     

    Short Cut Keys

     

    Private Sub CommandButton1_Click()
    'Copy the value in K5 cell and fills upto K10
    Worksheets("Sheet1").Range("K5:K10").FillDown
    'Copy the value in K5 and fills upto A5
    Worksheets("Sheet1").Range("K5:F5").FillLeft
    'Copy the value in K5 and fills upto P5
    Worksheets("Sheet1").Range("K5:P5").FillRight
    'Copy the value in K5 and fills upto K1
    Worksheets("Sheet1").Range("K5:K1").FillUp
    End Sub

    Range . Item

     

  • range.Item(Row index,Column Index)
  • MsgBox Range("A1:A5").Item(2)