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

x
?
Solved

Find a column and move/delete it

Posted on 2012-04-09
4
Medium Priority
?
212 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
[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
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
Anuroopsundd earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

705 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