Consulting

Results 1 to 4 of 4

Thread: Array help, deleting columns.

  1. #1

    Array help, deleting columns.

    I have the following macro which looks for the column name and deletes columns that are not in the Array List. The problem I have is that in the sheet that I am working off from, some of the column name is weird. It looks like this:

    Price
    (per thousand)

    as oppose to Price(per thousand)

    I think it is written that way to so that the per thousand part will show underneathe the Price. So with my macro it has to be exact spelling, but I don't know how to reflect that extra line in the macro. When I copy and paste it, it won't work. Please help.


    [VBA]
    cStay = Array("Name", "Address", "Zip (Within Select)", "Price
    (per thousand)")
    For Each rCell In Range(Cells(4, 1), _
    Cells(4, Columns.Count).End(xlToLeft))
    With rCell
    For i = 0 To UBound(cStay)
    If .Value = cStay(i) Then
    bStay = True
    Exit For
    End If
    Next i
    If Not bStay Then
    If rDel Is Nothing Then
    Set rDel = .Cells
    Else
    Set rDel = Union(rDel, .Cells)
    End If
    Else
    bStay = False
    End If
    End With
    Next rCell
    If Not rDel Is Nothing Then rDel.EntireColumn.Delete
    End If
    End Sub
    [/VBA]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    cStay = Array("Name", "Address", "Zip (Within Select)", "Price " & Chr(10) & "(per thousand)")
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Quote Originally Posted by xld
    [vba]

    cStay = Array("Name", "Address", "Zip (Within Select)", "Price " & Chr(10) & "(per thousand)")
    [/vba]
    thank you, that worked perfect!

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi mszuga,
    Use the green VBA button to format your code. "Code" tags don't work here.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •