Solved

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

Posted on 2013-01-10
9
427 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 34

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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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 34

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

734 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