You could try this code to perform such a task.
Sub FindMatchAndThenDeleteColumn()
FindContent = Worksheets("Sheet2").Range("J19")
For Each Cell In Worksheets("Sheet1").UsedRange.Cells
If Cell.Value = FindContent Then Columns(Cell.Column).Delete
Next
End Sub
Put the code above in your workbook's module. Note that FindContent
and Cell
are randomly chosen here, you can use any names. I use
For Each Cell In Worksheets("Sheet1").UsedRange.Cells
to loop through every cell that is in use in Sheet1. It will check everything in the range from cell A1 to the last cell with data (the bottom right-most cell). If the content of cell J19 is a text, you can declare the variable FindContent
as a String
type, i.e. Dim FindContent As String
. If it's a number, you can declare it as a Long
type or a Single
type or any number type that fits the content. Here I don't declare it which means it defaulting to a Variant
type. And since you're a beginner in VBA, you may learn it from Excel Easy. Hope this helps.