martedì 8 dicembre 2015

Excel VBA - some snippets

Questo post ha il solo scopo di tenere a portata di mano alcune funzioni VBA che uso abbastanza spesso.

This post is only meant to collect some VBA functions that I often use.

'
' Bottom Right corner of range
' Angolo inferiore destro di un range
'
Function LastCellInRange(rRange As Range) As Range
  Set rRange = rRange.Cells(rRange.Rows.cont, Range.Columns.Count)
End Function
'
' Expand Range to non empty row on first column and last column of range
' Espande il range fino alla prima cella non vuota nella prima colonna, e all'ultima colonna del range 
'
Function ExpandRange(rStartRange As Range) As Range

  If rStartRange.Cells(2, 1).empty Then
  ' Range composto da una solo rig
    Set ExpandRange= LastCellInRange(rStartRange)
  Else
    Dim r As Long, c As Integer
    r = rStartRange.Cells(1, 1).End(xlDown).Row
    c = rStartRange.Columns(rStartRange.Columns.Count).Column
    Set ExpandRange= Range(rStartRange.Cells(1, 1), Cells(r, c))
  End If
End Function