The Select method allows you to highlight a specific range on your worksheet. This is particularly useful when you want to visually indicate which cells your macro is working with.
Range("A1:C6").Select
Private Sub SelectDataRange()
'Selects cells from A1 to C6
Range("A1:C6").Select
'Alternative syntax using Cells property
'Range(Cells(1, 1), Cells(6, 3)).Select
End Sub
Pro Tip: While .Select is useful for demonstration purposes, in production code it's often better to work directly with ranges without selecting them, as this makes your code run faster.
The Columns property lets you work with specific columns within a defined range. This is extremely useful when you need to format or manipulate data in particular columns.
Private Sub FormatThirdColumn()
'Selects the third column in range A1:C6
Range("A1:C6").Columns(3).Select
'Alternative method using column letter
'Range("A1:C6").Columns("C").Select
End Sub
The With...End With structure is a VBA best practice that makes your code cleaner, easier to read, and more efficient by reducing repetitive object references.
Private Sub FormatSecondColumn()
With Range("A1:C6").Columns(2)
'Font formatting
.Font.Name = "Calibri"
.Font.Size = 12
.Font.Bold = True
.Font.Italic = True
.Font.Color = RGB(0, 0, 255) 'Blue color
'Cell formatting
.Interior.Color = RGB(255, 255, 200) 'Light yellow
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
'Borders
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlThick
End With
End Sub
Similar to the Columns property, the Rows property allows you to work with specific rows within a range.
Private Sub HighlightImportantRows()
'Format the header row (row 1)
With Range("A1:F20").Rows(1)
.Font.Bold = True
.Interior.Color = RGB(200, 200, 200) 'Gray background
.Font.Color = RGB(0, 0, 0) 'Black text
End With
'Format alternating rows for better readability
Dim i As Integer
For i = 2 To 20 Step 2
Range("A1:F20").Rows(i).Interior.Color = RGB(240, 240, 240)
Next i
End Sub
Often you need to work with ranges whose size isn't known in advance. These techniques help you work with dynamic data ranges.
Private Sub WorkWithDynamicRange()
Dim lastRow As Long
Dim ws As Worksheet
Set ws = ActiveSheet
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'Now work with the dynamic range
With ws.Range("A1:C" & lastRow)
.Font.Name = "Arial"
.Columns.AutoFit
End With
End Sub
Private Sub FormatNonContiguousRanges()
'Combine multiple areas into one range
Dim multiRange As Range
Set multiRange = Union(Range("A1:A10"), Range("C1:C10"), Range("E1:E10"))
With multiRange
.Font.Bold = True
.Interior.Color = RGB(255, 230, 230) 'Light red
End With
End Sub
Private Sub AddDataValidation()
With Range("B2:B20").Validation
.Delete 'Clear any existing validation
.Add Type:=xlValidateWholeNumber, _
AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, _
Formula1:="1", _
Formula2:="100"
.InputTitle = "Enter Score"
.ErrorTitle = "Invalid Score"
.InputMessage = "Please enter a value between 1 and 100"
.ErrorMessage = "You must enter a number between 1 and 100"
.ShowInput = True
.ShowError = True
End With
End Sub
Private Sub CreateSummary()
Dim srcRange As Range, destRange As Range
Dim lastRow As Long
'Find the last row with data
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
'Define source and destination ranges
Set srcRange = Range("A2:B" & lastRow)
Set destRange = Range("D1")
'Create summary table
destRange.Value = "Category"
destRange.Offset(0, 1).Value = "Count"
destRange.Offset(0, 2).Value = "Average"
'Add formulas
srcRange.Columns(1).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=destRange.Offset(1, 0), _
Unique:=True
'Count and average formulas
Dim uniqueCount As Long
uniqueCount = WorksheetFunction.CountA(Range("D2:D100"))
With destRange.Offset(1, 1).Resize(uniqueCount, 2)
.Columns(1).FormulaR1C1 = "=COUNTIF(C2:C" & lastRow & ",RC[-1])"
.Columns(2).FormulaR1C1 = "=AVERAGEIF(C2:C" & lastRow & ",RC[-2],C3:C" & lastRow & ")"
.NumberFormat = "0.00"
End With
'Format the summary table
With destRange.CurrentRegion
.Font.Bold = True
.Borders.LineStyle = xlContinuous
.Columns.AutoFit
End With
End Sub
Copyright ® 2008- Dr.Liew Voon Kiong . All rights reserved [Privacy Policy]
Contact: Facebook Page