Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Find a column and move/delete it

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
swjtx99
Asked:
swjtx99
  • 2
  • 2
1 Solution
 
AnuroopsunddCommented:
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
 
swjtx99Author Commented:
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
 
AnuroopsunddCommented:
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
 
swjtx99Author Commented:
Great solution but I might not have asked the right quesiton.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now