Delete Columns to the left and above a column name

zalik22

Board Regular
Joined
Dec 14, 2010
Messages
122
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a cell labeled "Agent Group" somewhere in my file and would like to delete everything above and to the left of it. Any help on how to do this?

Thanks!
 

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.
Re: Delete Columns to the left and above a coulmn name

Do you mean delete entire rows & columns, so that Agent Group becomes cell A1?
 
Upvote 0
Re: Delete Columns to the left and above a coulmn name

Yes please!
 
Upvote 0
Re: Delete Columns to the left and above a coulmn name

Here is some code that should do that:
Code:
Sub MyMacro()

    Dim fCell As Range

'   Find Agent Group
    On Error GoTo err_chk
    Cells.Find(What:="Agent Group", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    On Error GoTo 0
    Set fCell = ActiveCell
    
'   Delete rows above
    If fCell.Row > 1 Then
        Rows("1:" & fCell.Row - 1).Delete
    End If
    
'   Delete columns to the left
    If fCell.Column > 1 Then
        Range(Cells(1, 1), Cells(1, fCell.Column - 1)).EntireColumn.Delete
    End If
    
    Exit Sub
    
err_chk:
    MsgBox "Cannot find the text 'Agent Group' on sheet", vbOKOnly, "ERROR!"
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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