Solved

Find a column and move/delete it

Posted on 2012-04-09
4
206 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

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…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

831 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