Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Loop through a row and delete all columns when a certain value is found

Posted on 2013-01-10
9
Medium Priority
?
430 Views
Last Modified: 2013-01-28
Hi Experts

I need to be able to loop through the column headings in Row 1 of a spreadsheet (starting from column C  i.e "C1" and find the first column which contains value "Old_Address" and then delete this columns and all columns  after this.  So for example if "Old_Address" was found in Cell H1 then I would want all the columns from H onwards to be deleted.  The number of columns will change everyday.

Your assistance would be appreciated.

Thanks
0
Comment
Question by:simsima_7876
  • 4
  • 3
  • 2
9 Comments
 
LVL 35

Expert Comment

by:Norie
ID: 38764192
Try this.
Dim varOldAdd As Variant
Dim LastCol As Long

    varOldAdd = Application.Match("Old_Address", Rows(1), 0)
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column

    If Not IsError(varOldAdd) Then
        Cells(1, varOldAdd).Resize(, LastCol - varOldAdd + 1).EntireColumn.Delete
    End If

Open in new window

0
 

Author Comment

by:simsima_7876
ID: 38764243
I get a error 2042 with this code even though the value is definitely there.
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38764267
Hi,  simsima_7876.

Please see attached. The code is...
Option Explicit

Sub Delete_Columns()
Dim xCol      As Range
Dim xLast_Col As Long
Dim i         As Long

Sheets("To Be Deleted").Activate

If ActiveSheet.UsedRange.Columns.Count > 0 Then xLast_Col = [A1].SpecialCells(xlLastCell).Column
If xLast_Col < 4 Then
    MsgBox ("No columns after C - run cancelled.")
    Exit Sub
End If

Set xCol = Range("1:1").Find(What:="Old_Address", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If xCol Is Nothing Then
    MsgBox ("""Old_Address"" not found.")
Else
    Range(Cells(1, xCol.Column), Cells(1, xLast_Col)).EntireColumn.Delete
    If ActiveSheet.UsedRange.Columns.Count < 0 Then Debug.Print "?!"
End If

End Sub

Open in new window

Regards,
Brian.
Delete-Columns.xls
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:simsima_7876
ID: 38764269
However in this case the value I am searching for is 41723 ( a numeric date) as opposed to the string Old_address
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38764289
Apologies, simsima_7876, I forgot to skip columns A:B. Please see attached.Delete-Columns-V2.xls
0
 
LVL 35

Expert Comment

by:Norie
ID: 38764314
simsima

Did you change the code I posted in anyway?
0
 
LVL 26

Accepted Solution

by:
redmondb earned 1600 total points
ID: 38764341
simsima,

The attached handles dates. The code is...
Option Explicit

Sub Delete_Columns()
Dim xCol      As Range
Dim xLast_Col As Long
Dim i         As Long
Dim xSearch   As Variant

Sheets("To Be Deleted").Activate

If ActiveSheet.UsedRange.Columns.Count > 0 Then xLast_Col = [A1].SpecialCells(xlLastCell).Column
If xLast_Col < 3 Then
    MsgBox ("No columns after B - run cancelled.")
    Exit Sub
End If

xSearch = DateSerial(2014, 3, 25)

Set xCol = Range(Cells(1, 3), Cells(1, xLast_Col)).Find(What:=xSearch, LookIn:=xlFormulas, LookAt:=xlWhole, MatchCase:=False)
If xCol Is Nothing Then
    MsgBox (xSearch & " not found.")
Else
    Range(Cells(1, xCol.Column), Cells(1, xLast_Col)).EntireColumn.Delete
    If ActiveSheet.UsedRange.Columns.Count < 0 Then Debug.Print "?!"
End If

End Sub

Open in new window

Regards,
Brian.Delete-Columns-V3.xls
0
 

Author Closing Comment

by:simsima_7876
ID: 38826970
Hi Redmond - sorry for the delay.  You arethe man!  Thanks for the solution - it worked like  a dream.
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38827029
Thanks, simsima!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question