VBA Modules for Excel Automation (Advance Excel)
Essential VBA Modules for Excel Automation 1. Auto-Adjust Column Width Automatically adjust all columns to fit content. Sub AutoFitColumns() Cells.EntireColumn.AutoFit End Sub 2. Delete Blank Rows Removes all empty rows in the active sheet. Sub DeleteBlankRows() Dim rng As Range Set rng = ActiveSheet.UsedRange Dim i As Integer For i = rng.Rows.Count To 1 Step -1 If WorksheetFunction.CountA(rng.Rows(i)) = 0 Then rng.Rows(i).Delete End If Next i End Sub 3. Highlight Duplicates Highlights duplicate values in a selected range. Sub HighlightDuplicates() Dim rng As Range Set rng = Selection rng.FormatConditions.AddUniqueValues rng.FormatConditions(1).DupeUnique = xlDuplicate rng.FormatConditions(1).Interior.Color = RGB(255, 0, 0) End Sub 4. Merge Multiple Sheets into One Merges all sheets into a single sheet. Sub MergeSheets() Dim ws As Worksheet, destSheet As Worksheet Set destSheet = Sheets.Add destSh...