Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error on multiple find/select do loop

Status
Not open for further replies.

Hosacans

Technical User
Dec 15, 2004
64
US
Hi All,

I have a spreadsheet with multiple columns that contains the same header. I want to write a macro that will delete the entire column with that header. here's what i have so far

Code:
    N(1) = "Pen %"
    N(2) = "LY R Sls "
    N(3) = "LY U Sls "
    N(4) = "Chg %"
    N(5) = "TY U EOH (DC) "
    N(6) = "TY R EOH (DC) "
    N(7) = "LY U EOH"
    N(8) = "LY U EOH (DC) "
    N(9) = "LY R EOH"
    N(10) = "LY R EOH (DC) "
    N(11) = "U Rcpts (DC)"
    N(12) = "R Rcpts (DC)"
    N(13) = "TY MD"
    N(14) = "TY MD %"
    N(15) = "LY MD"
    N(16) = "LY MD %"
    N(17) = "GM %"
    N(18) = "GM"
    N(19) = "S/T (STRS)"
    N(20) = "S/T (All STRS + DC)"
    
For a = 1 To 20

    Do
    
    On Error GoTo nextA
    
    Cells.Find(What:=N(a), After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False).Activate
   
    
    ActiveCell.EntireColumn.Select
    Selection.Delete Shift:=xlToLeft
    
   
    
    Loop

nextA:

Next a

but after going through the first instance "Pen %" i got a error on "LY R Sls"

Runtime error '91'
Object Variable or With block variable not set.

Please advise, i've been going at it with error handling for hours. it seems like the bug comes from within cell.find command. and somehow "LY R Sls" is out of range, but if that's the case, then how can "Pen % get through the loop fine and deleted all the columns with that header?

Thanks so much
Hosackies
 




Hi,

try this...
Code:
dim found as range

For a = 1 To 20

    Do
    
    On Error GoTo nextA
    
    Set found = Cells.Find(What:=N(a))
   
   If Not found Is Nothing Then
    
        ActiveCell.EntireColumn.Select
        Selection.Delete Shift:=xlToLeft
    
   End If
    
    Loop

nextA:

Next a

Skip,
[sub]
[glasses] When a wee mystic is on the loose..
It's a Small Medium at Large! [tongue][/sub]
 
Skip,

This is putting it through an infinite loop and deletes all the data.

Please advise.

Thanks
Hosackies
 




Sorry, I was not paying the attention i should have...
Code:
Dim found As Range

For a = 1 To 20

    Do
    
    On Error GoTo nextA
    
    Set found = Cells.Find(What:=N(a))
   
   If Not found Is Nothing Then
    
        found.EntireColumn.Delete Shift:=xlToLeft
    Else
        Exit Do
   End If
    
    Loop

nextA:

Next a

Skip,
[sub]
[glasses] When a wee mystic is on the loose..
It's a Small Medium at Large! [tongue][/sub]
 
Thanks Skip,

That worked wonderfully.
Hosacans
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top