Solved

How do I delete a column in Excel based on a cell value?

Posted on 2011-09-20
2
254 Views
Last Modified: 2012-05-12
Hello Experts.

I have a large spreadsheet and would like a macro that will delete any columns that have "MOE" in row 1, and shift the columns so that there aren't any blank columns.

Also, the number of columns will vary on each spreadsheet that I will modify with this macro.  

Please let me know if any additional information is required.

Thanks,
Jerald
0
Comment
Question by:ferguson_jerald
[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
2 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 500 total points
ID: 36571638
Assuming that a blank column ends the sequence, this should work:

 
Sub RemoveSelectColumns()
    Dim iColumn As Integer
    Dim strCell As String
    Dim strColumnName As String
    Dim r As Range
    
    iColumn = 1
    Do
        strCell = Cells(1, iColumn).Value
        If strCell = "" Then
            Exit Do
        End If
        If UCase(strCell) = "MOE" Then
            ' case-independent
            strColumnName = Split(Cells(1, iColumn).Address, "$")(1)
            Columns(strColumnName & ":" & strColumnName).Delete Shift:=xlToLeft
        Else
            iColumn = iColumn + 1
        End If
    Loop
End Sub

Open in new window

0
 

Author Closing Comment

by:ferguson_jerald
ID: 36571683
Exactly what I needed.  Thanks!
0

Featured Post

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.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

751 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