Excel VBA Tutor Home Excel VBA Classic Excel VBA 2010 Excel VBA 365 Excel VBA Examples About Us

Excel VBA Lesson 18: Mastering Excel VBA Methods


In Excel VBA, methods are actions that can be performed on objects. While properties describe what objects are, methods describe what objects do. This lesson covers essential methods for working with ranges and cells in Excel.

18.1 The ClearContents Method

The ClearContents method removes data from cells while preserving formatting. This is different from deleting cells, which shifts surrounding cells.

Example 18.1: Basic ClearContents

Private Sub CommandButton1_Click()
    Range("A1:A6").ClearContents
End Sub

This clears values from cells A1 through A6.

Example 18.2: User-Defined Range Clear

Private Sub CommandButton1_Click()
    Dim selectedRng As String
    selectedRng = InputBox("Enter range to clear (e.g., B2:D10)")
    If selectedRng <> "" Then
        Range(selectedRng).ClearContents
    End If
End Sub

Added error handling for when user cancels the input box.

Example 18.2a: Clear Entire Worksheet

Sub ClearAllData()
    Dim response As Integer
    response = MsgBox("Clear ALL data in this sheet?", vbYesNo + vbQuestion)
    If response = vbYes Then
        Sheet1.Cells.ClearContents
        MsgBox "Worksheet cleared successfully", vbInformation
    End If
End Sub

Safer implementation with confirmation dialog.

18.2 The ClearFormats Method

ClearFormats removes formatting while keeping cell values intact. Useful for standardizing appearance.

Example 18.2b: Clear Formats with Undo Option

Sub RemoveAllFormatting()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = ActiveSheet
    ws.Cells.ClearFormats
    ws.Cells.Font.Name = "Calibri"
    ws.Cells.Font.Size = 11
    Application.ScreenUpdating = True
    MsgBox "All formatting cleared. Default font applied.", vbInformation
End Sub

18.3 The Select Method

While Select is commonly used, direct range manipulation is often more efficient in VBA.

Example 18.3: Efficient Selection Techniques

Sub SelectRangeExamples()
    ' Basic selection
    Range("A1:D10").Select
    
    ' Select entire used range
    ActiveSheet.UsedRange.Select
    
    ' Select current region (contiguous data)
    Range("A1").CurrentRegion.Select
    
    ' Select with Offset (3 rows down, 2 columns right)
    Range("A1").Offset(3, 2).Select
    
    ' Select last cell in column A
    Cells(Rows.Count, 1).End(xlUp).Select
End Sub

18.4 VBA Method Best Practices

  • Avoid excessive Select: Directly reference ranges instead of selecting them first
  • Combine methods: Chain methods together like Range("A1").Font.Bold = True
  • Error handling: Always include error handling when working with ranges
  • Optimize performance: Use Application.ScreenUpdating = False for multiple operations

Practical Application: Data Cleanup Macro

Example 18.9: Comprehensive Data Preparation

Sub PrepareDataForAnalysis()
    On Error GoTo ErrorHandler
    Application.ScreenUpdating = False
    
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    ' Clear old data and formatting
    ws.Cells.ClearContents
    ws.Cells.ClearFormats
    
    ' Apply standard formatting
    With ws.UsedRange
        .Font.Name = "Calibri"
        .Font.Size = 11
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlCenter
    End With
    
    ' Format headers
    With Rows(1)
        .Font.Bold = True
        .Interior.Color = RGB(200, 200, 200)
    End With
    
    Application.ScreenUpdating = True
    MsgBox "Data preparation complete!", vbInformation
    Exit Sub
    
ErrorHandler:
    Application.ScreenUpdating = True
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical
End Sub

Summary

✅ In This Lesson, You Learned:

  • ClearContents removes cell values while preserving formatting
  • ClearFormats removes formatting while keeping values
  • Select highlights ranges but direct reference is often better
  • AutoFill creates series (numbers, dates, custom lists)
  • Combine methods with properties for powerful Excel automation
  • Always include error handling when working with ranges

🔗 Related Resources



Copyright ® 2008- Dr.Liew Voon Kiong . All rights reserved   [Privacy Policy]

Contact: Facebook Page