get a range of numbers with vba excel code example

Example 1: excel vba can a vba function return a range?

'VBA functions can return an Excel range, but since a range
'is an object, the 'Set' keyword is required:

Function Test() As Range
    Set Test = [A1:Z99]
End Function

Example 2: excel vba get a range of full columns

To get a contiguous range of entire rows, use this VBA function:

Function WSRows(ws As Worksheet, Row1&, Rows&)
    Set WSRows = ws.Rows(Row1).Resize(Rows)
End Function
  
'-----------------------------------------------------------------
  
MsgBox WSRows(Sheet1, 11, 9).Address		'<--displays: $11:$20
  
'To get the values in the range into a variant array:  
v =  WSRows(Sheet1, 11, 9)
   
'-----------------------------------------------------------------
 
  
'Sister function to a get range of contiguous full columns:  
  
Function WSCols(ws As Worksheet, Col1&, Cols&)
    Set WSCols = ws.Columns(Col1).Resize(, Cols)
End Function

Tags:

Vb Example