vba column count range code example

Example 1: count number of cell ina range vba

Sub DisplayColumnCount() 
    Dim iAreaCount As Integer 
    Dim i As Integer 
 
    Worksheets("Sheet1").Activate 
    iAreaCount = Selection.Areas.Count 
 
    If iAreaCount <= 1 Then 
        MsgBox "The selection contains " & Selection.Columns.Count & " columns." 
    Else 
        For i = 1 To iAreaCount 
            MsgBox "Area " & i & " of the selection contains " & _ 
            Selection.Areas(i).Columns.Count & " columns." 
        Next i 
    End If 
End Sub

Example 2: vba count columns

Sub Range_End_Method()
'Finds the last non-blank cell in a single row or column

Dim lRow As Long
Dim lCol As Long
    
    'Find the last non-blank cell in column A(1)
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    'Find the last non-blank cell in row 1
    lCol = Cells(1, Columns.Count).End(xlToLeft).Column
    
    MsgBox "Last Row: " & lRow & vbNewLine & _
            "Last Column: " & lCol
  
End Sub

Tags:

Vb Example