Delete adjacent columns

doncarp

Board Regular
Joined
Aug 21, 2004
Messages
55
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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
11,946
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
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
 

doncarp

Board Regular
Joined
Aug 21, 2004
Messages
55
Office Version
  1. 2016
Platform
  1. Windows
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.
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
11,946
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
My pleasure. :)
 

Forum statistics

Threads
1,170,932
Messages
5,872,780
Members
432,945
Latest member
eddyevad

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
Top