Solved

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

Posted on 2013-01-10
9
425 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 33

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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 33

Expert Comment

by:Norie
ID: 38764314
simsima

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

Accepted Solution

by:
redmondb earned 400 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

749 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