Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-20
2
Medium Priority
?
266 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
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…
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 …

688 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