Results 1 to 4 of 4

Thread: Deleting rows from an excel file

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    5

    Unhappy Deleting rows from an excel file

    Hi,

    was hoping someone could help me with my project. Im trying to delete a specific number of rows from an excel file using:

    Counter = num_of_lines_read
    Do While (Counter > 0) And (Counter = num_of_lines_read)
    xlBook.Worksheets("Sheet").Row(1).delete
    Loop

    basically if i have read up to 8 lines i want to delete these lines. Problem is i keep gettin up an error for the line
    xlBook.Worksheets("Sheet").Row(1).delete
    can anyone tell me whats wrong, if anything with that line?
    the error i get is error 9 subscript out of range.

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Does a Sheet named "Sheet" exist?
    Is your xlBook correctly put intoio live?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    In this case, a subscript out of range error would have to be on the subscript passed to Worksheets. The means the workbook does not containa worksheet named "Sheet". You can reference a worksheet by either index (Example: Worksheets(1)) or name (Example: Worksheets("Sheet1").

    In the little worksheet tabs at the bottom of Excel, the Index is the order in wich the sheets appear and the name is the name on the tab.

    You also want to use the property "Rows", not "Row".

    And your Do..Loop will never end or never be called because neither Counter nor num_of_lines_read change and even if there were a change to either variable the loop would terminate because then Counter <> num_of_lines_read, so Do can only and will always endlessly loop when num_of_lines_read >0 otherwise it will always terminate. So you will get endless loop or nothing.

  4. #4
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068
    Here's how I do it....
    VB Code:
    1. For i = 5 To 400
    2.             If Range("$C$" & i).Text = "" Then
    3.                 MsgBox "All Non Oc agents have been removed from the list", vbInformation, "Done!"
    4.                     Exit Sub
    5.             End If
    6.  
    7.             If InStr(1, Oc, Range("$C$" & i).Text) = False Then
    8.                 Range("$C$" & i).EntireRow.Delete
    9.                 i = i - 1
    10.             End If
    11.         Next i
    That's from a workbook I use here at work to remove certain employees that I don't care about... LoL, well their stats anyways haha.

Posting Permissions

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



Click Here to Expand Forum to Full Width