ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Deleting Columns Based upon Heading (https://www.excelbanter.com/excel-programming/344694-deleting-columns-based-upon-heading.html)

Jessica

Deleting Columns Based upon Heading
 
I am writing a macro where I need to delete certain columns. I can write the
code to delete columns by the letter (i.e., if I want to delete column C).
However I want to delete a column based upon the heading.

For instance, I want to delete the column entitled "Bidder". I can write a
macro to find the header "Bidder", but I am not sure how to indicate that I
want the column that the active cell is in to be deleted.



Norman Jones

Deleting Columns Based upon Heading
 
Hi Jessica,

Assume your headers are in row 1, try:
:
'=============
Public Sub Tester()
Dim Rng As Range

Set Rng = Rows(1).Find(What:="Bidder", _
After:=Range("A1"), _
MatchCase:=False)

If Not Rng Is Nothing Then Rng.EntireColumn.Delete

End Sub
'<<=============

Change Rows(1) to accord with the header row.

---
Regards,
Norman



"Jessica" wrote in message
...
I am writing a macro where I need to delete certain columns. I can write
the
code to delete columns by the letter (i.e., if I want to delete column C).
However I want to delete a column based upon the heading.

For instance, I want to delete the column entitled "Bidder". I can write
a
macro to find the header "Bidder", but I am not sure how to indicate that
I
want the column that the active cell is in to be deleted.





Jim Thomlinson[_4_]

Deleting Columns Based upon Heading
 
Give this a whirl...

Sub Test()
DeleteColumns "Bidder"
DeleteColumns "That"
End Sub

Sub DeleteColumns(ByVal strHeader As String)
Dim rngFound As Range
Dim wks As Worksheet

Set wks = ActiveSheet

Set rngFound = wks.Rows(1).Find(strHeader)

If rngFound Is Nothing Then
MsgBox strHeader & " was not found."
Else
rngFound.EntireColumn.Delete
End If

End Sub
--
HTH...

Jim Thomlinson


"Jessica" wrote:

I am writing a macro where I need to delete certain columns. I can write the
code to delete columns by the letter (i.e., if I want to delete column C).
However I want to delete a column based upon the heading.

For instance, I want to delete the column entitled "Bidder". I can write a
macro to find the header "Bidder", but I am not sure how to indicate that I
want the column that the active cell is in to be deleted.




All times are GMT +1. The time now is 06:49 PM.

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