I have one file in which there are many columns.I dont want data of Budget column.How to do it through vba code
Sub ClearBudgetColumnIn2024TotalSheet()
Dim ws As Worksheet
Dim lastRow As Long
Dim budgetColumn As Long
Dim rng As Range
' Check if the sheet named "2024Total" exists
On Error Resume Next
Set ws = ThisWorkbook.Sheets("2024Total")
On Error GoTo 0
If ws Is Nothing Then
MsgBox "Sheet named '2024Total' not found!", vbExclamation
Exit Sub
End If
' Find the last row of data in the "2024Total" sheet
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Find the column index of "Budget" in the "2024Total" sheet
budgetColumn = 0
For i = 1 To ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
If ws.Cells(1, i).Value = "Budget" Then
budgetColumn = i
Exit For
End If
Next i
' Check if the "Budget" column exists in the "2024Total" sheet
If budgetColumn > 0 Then
' Clear the cells in the "Budget" column for each row (excluding the header row)
Set rng = ws.Range(ws.Cells(2, budgetColumn), ws.Cells(lastRow, budgetColumn))
rng.ClearContents
Else
MsgBox "Budget column not found in sheet: 2024Total", vbExclamation
End If
End Sub
New contributor
Khushbu Yadav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.