Solved

Find a column and move/delete it

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
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…

830 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