Delete adjacent columns

doncarp

Board Regular
Joined
Aug 21, 2004
Messages
57
Office Version
  1. 2016
Platform
  1. Windows
I need to improve a portion of my macro that is supposed to delete two columns. When my estimating program exports to Excel there is a column named "Digitizer" and the next column does not have a header. Digitizer is not always in the same column. I need to find "Digitizer"and delete two columns. I thought I had it with this code, but today I had a case where there was some data in the column and I only deleted some cells, not the entire column.

Dim rng1 As Range
Set rng1 = Range(Range("A1:Z1").Find("Digitizer").Offset(0, 1), Range("A1:Z1").Find("Digitizer").End(xlDown))
rng1.Select
Selection.Delete Shift:=xlToLeft
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
If I understood you correctly, this code will delete the "Digitizer" column and the column to the right.
Code:
Sub DelCol()
    Dim rng1 As Range
    With Sheets("Sheet1").Rows(1)
        Set rng1 = .Find(What:="Digitizer", _
                After:=.Cells(.Cells.Count), _
                LookIn:=xlValues, _
                LookAt:=xlWhole, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, _
                MatchCase:=False)
        If Not rng1 Is Nothing Then
            Columns(rng1.Column).Resize(, 2).EntireColumn.Delete
        End If
    End With
End Sub
 
Upvote 0
You understood exactly what I needed. I did not have a chance to test it until this morning. I changed With Sheets("Sheet1").Rows(1) to With ActiveSheet.Rows(1) and it worked perfectly. You are amazing. Thank you.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top