ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Delete columns (https://www.excelbanter.com/excel-programming/388811-delete-columns.html)

Mike D.

Delete columns
 
Hi. I'm looking for a way to delete a series of columns, starting in column
"B". I want to search for the word, "Total" in row 1. I want to delete all
columns up to the column with the word "Total in it. For instance, If J1
contains "Total", I would want to delete columns B through I.

Thanks,
Mike.

joel

Delete columns
 
Try This


Sub DeleteColumns()

LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
Set HeaderRange = Range(Cells(1, 2), Cells(1, LastColumn))
Set c = HeaderRange.Find("Total", LookIn:=xlValues)

If Not c Is Nothing Then

Set DeleteRange = Range(Cells(1, 2), _
Cells(1, c.Column - 1))
DeleteRange.EntireColumn.Delete
End If

End Sub

"Mike D." wrote:

Hi. I'm looking for a way to delete a series of columns, starting in column
"B". I want to search for the word, "Total" in row 1. I want to delete all
columns up to the column with the word "Total in it. For instance, If J1
contains "Total", I would want to delete columns B through I.

Thanks,
Mike.


Gord Dibben

Delete columns
 
With the addition of this line ............

Dim LastColumn As Long below Sub DeleteColumns()


Gord Dibben MS Excel MVP

On Sat, 5 May 2007 13:34:00 -0700, Joel wrote:

Try This


Sub DeleteColumns()

LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
Set HeaderRange = Range(Cells(1, 2), Cells(1, LastColumn))
Set c = HeaderRange.Find("Total", LookIn:=xlValues)

If Not c Is Nothing Then

Set DeleteRange = Range(Cells(1, 2), _
Cells(1, c.Column - 1))
DeleteRange.EntireColumn.Delete
End If

End Sub

"Mike D." wrote:

Hi. I'm looking for a way to delete a series of columns, starting in column
"B". I want to search for the word, "Total" in row 1. I want to delete all
columns up to the column with the word "Total in it. For instance, If J1
contains "Total", I would want to delete columns B through I.

Thanks,
Mike.



Mike D.

Delete columns
 
Thanks, Joel!

"Joel" wrote:

Try This


Sub DeleteColumns()

LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
Set HeaderRange = Range(Cells(1, 2), Cells(1, LastColumn))
Set c = HeaderRange.Find("Total", LookIn:=xlValues)

If Not c Is Nothing Then

Set DeleteRange = Range(Cells(1, 2), _
Cells(1, c.Column - 1))
DeleteRange.EntireColumn.Delete
End If

End Sub

"Mike D." wrote:

Hi. I'm looking for a way to delete a series of columns, starting in column
"B". I want to search for the word, "Total" in row 1. I want to delete all
columns up to the column with the word "Total in it. For instance, If J1
contains "Total", I would want to delete columns B through I.

Thanks,
Mike.



All times are GMT +1. The time now is 05:39 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com