VBA & Macro Problems

Excel VBA: Find a Cell Value and Delete the Matching Column

Olivia MillerOlivia Miller Nov 25, 2026 870 views

Question details

Use a value in cell J19 on one worksheet to find and delete a matching column on another worksheet.

Find a Value and Delete the Matching Column in Excel VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
Cell J19 is on Sheet2, and the macro should search for its value on Sheet1.
Observed behavior
The desired result is to delete the column containing the matching value on Sheet1 without an error if no match is found.
Before you start

Confirm that the source sheet is named Sheet2, the search sheet is named Sheet1, and J19 contains the value to find. The macro deletes an entire column and cannot be undone with Excel's Undo command.

Solution 1Recommended

Find the J19 value and delete its matching column with VBA

Use this macro to search Sheet1 for the value in Sheet2!J19 and delete the first matching column.

The search uses whole-cell matches and is not case-sensitive. If the value appears more than once, only the column containing the first match found is deleted.

1
Open the VBA editor

In Excel, open the workbook containing both worksheets, then select Developer > Visual Basic.

2
Insert the macro

In the VBA editor, select Insert > Module and paste this code: Sub DeleteMatchingColumn() Dim targetValue As Variant Dim hit As Range targetValue = ThisWorkbook.Worksheets("Sheet2").Range("J19").Value If IsError(targetValue) Then MsgBox "Sheet2!J19 contains an error." Exit Sub End If If Len(CStr(targetValue)) = 0 Then MsgBox "Sheet2!J19 is empty." Exit Sub End If Set hit = ThisWorkbook.Worksheets("Sheet1").Cells.Find( _ What:=targetValue, LookIn:=xlValues, LookAt:=xlWhole, _ SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If hit Is Nothing Then MsgBox "No matching value was found on Sheet1." Else hit.EntireColumn.Delete End If End Sub

3
Run the macro

Return to Excel, select Developer > Macros, choose DeleteMatchingColumn, and click Run. If your worksheet names differ, change "Sheet2" and "Sheet1" in the code first.

4
Save the macro if needed

To keep the macro in the workbook, select File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm).

Find the J19 value and delete its matching column with VBA
Check the macro location: ThisWorkbook refers to the workbook containing the macro. Place the macro in the workbook that contains Sheet1 and Sheet2. A protected worksheet may prevent the column from being deleted.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface and Microsoft Office format compatibility for an easier transition. VBA support can vary by version and configuration, so verify macro compatibility before relying on this Excel macro in WPS.

Open and edit spreadsheets in a familiar interfaceCompatible with Microsoft Excel formats such as XLSXFind values and manage spreadsheet columnsLightweight suite for everyday document work
microsoft office alternative - wps office

Frequently Asked Questions

What happens if the value in J19 is not found?

The macro checks whether Find returned Nothing. If there is no match, it displays a message and leaves the columns unchanged.

How do I search the other way around?

In the macro, change the source reference to ThisWorkbook.Worksheets("Sheet1").Range("J19") and the search reference to ThisWorkbook.Worksheets("Sheet2").Cells.

Does the macro delete every column containing the value?

No. It deletes only the column containing the first whole-cell match found on Sheet1.

Why can't I run the macro?

Check Excel's security warning and enable macros only if you trust the workbook. If you need to retain the code, use File > Save As and save the workbook as an XLSM file.