Solved

Find a column and move/delete it

Posted on 2012-04-09
4
204 Views
Last Modified: 2012-06-21
Hi,

I need to identify a column by the column header name (cell 1) and either move it or delete it. (sometimes one, sometimes the other)

I get a sheet daily where the columns are not always the same. I know it's crazy but sometimes the column with "Name" in the header is column E and sometimes it's column F.

In addition, sometimes "CITY" is in the column after "Name" and sometimes there will be a blank column (or two) in between.


So sometimes it's like this:

A               B                C                     D
Name                       City

and sometimes it's like this:

A               B                C                     D
             Name                                 City

The data is always there, but can't predict the column. I'd like to sort this out with a macto and just get this:

A               B                C                     D
Name     City          


The first part would be to look at row 1 and delete any columns where the cell in row 1 is blank.

The seond part of this solution would be code that would look aross row 1, find the cell that contains the data "Name" and move it to column C........

Also has to run in VB as a macro.

If I had that, I could build the columns I wanted out to the right of everything in the order I wanted, with no random empty columns then delete all colums to the left of my first column.

I hope this makes sense and thanks in advance,

swjtx99
0
Comment
Question by:swjtx99
  • 2
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
Anuroopsundd earned 500 total points
ID: 37826148
Option Explicit

Sub MoveColumns()
    Dim aCols() As Variant, z As Long, iColCnt As Long
    Dim rFind As Range, rLook As Range
    aCols = Array("Name", "City")
    Set rLook = ActiveSheet.Range("1:1")
    For z = LBound(aCols) To UBound(aCols)
        Set rFind = rLook.Find(What:=aCols(z))
        If Not rFind Is Nothing Then
            If ActiveSheet.Columns(z + 1).Address <> rFind.EntireColumn.Address Then
                rFind.EntireColumn.Cut
                ActiveSheet.Columns(z + 1).Insert
            End If
        End If
    Next z
    Application.CutCopyMode = False
End Sub



http://forums.techguy.org/business-applications/871116-excel-2007-macro-newbie-move.html
0
 

Author Comment

by:swjtx99
ID: 37826181
Hi Anuroopsundd,

Thanks for the fast reply. That is exactly what I asked for but I'm not sure how to costomize it. I actually have 15 to 20 columns on this report and need to move some, delete some and at the end, get all in a specific order and I was hoping there was a less efficient but more intuitive way to do this so that by asking for only a couple of examples I could build on it to complete it and even customize it a different way as the requirements changed so really what I would like is :

1. How to find a column where the header is "Name" and move it to some other column
2. How to find a column where the header is "Name" and delete it.

With those 2 examples, I could just change the column name to "City", "State", "Country" or whatever and either move it or delete it as necessary.

I'm afraid I'm not familiar enough with VB to do that with what you wrote, as good as it certainly is.

Is there an easier way?

Thanks,

swjtx99
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37826190
To Delete a column with heading Country..

Sub DeleteColumns()
    Dim i As Integer, A As Range
    For i = 10 To 1 Step -1
        Set A = Cells(1, i).Find(What:="Country", LookIn:=xlValues)
        If Not A Is Nothing Then A.EntireColumn.Delete
    Next i
End Sub
0
 

Author Closing Comment

by:swjtx99
ID: 37827071
Great solution but I might not have asked the right quesiton.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now