Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9245645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:18:41+00:00 2026-06-18T09:18:41+00:00

Sooo, i had a nice little subroutine that would remove all but a single

  • 0

Sooo, i had a nice little subroutine that would remove all but a single row in a table.

Sub ClearFormTableRows(sourceRange, countRange)
Dim rowsCount As Integer, rowRangeName As Object
rowsCount = Application.WorksheetFunction.CountA(countRange)
With sourceRange

    Do Until rowsCount = 1

        .ListObject.ListRows(2).Delete
        rowsCount = rowsCount - 1
        If rowsCount = 1 Then Exit Do
    Loop

End With
End Sub

And until today it worked. But in the main routine that calls it i added another call, same syntax just for a different table and now it wont do anything, throws an error 9 subroutine out of range.
The row it highlights is this one: .ListObject.ListRows(2).Delete
But, if i comment out either of the calls, it doesn’t matter which, it will execute properly on that table. But ask it to do both and it wont do either.

What am i doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:18:42+00:00Added an answer on June 18, 2026 at 9:18 am

    Try in this way: I removed rowRangeName object as it’s not in use for given snippet in your question.

      Sub ClearFormTableRows(ByRef sourceRange as Range, ByRef countRange As Range)
         Dim rowsCount As Integer
         Dim objListObj As ListObject 
         Dim objListRows As ListRows 
    
         Set objListObj = sourceRange.ListObjects(1) 
         Set objListRows = objListObj.ListRows 
         rowsCount = Application.WorksheetFunction.CountA(countRange)
    
         While rowsCount > 1
         '-- just a re-confirmation, you can remove first if condition if you want.
         If (rowsCount > 1) And (rowsCount < objListRows.Count - 1) Then 
            objListRows(rowsCount).Delete 
            rowsCount = rowsCount - 1
         End If 
         Wend
      End Sub
    

    Reference:

    • ListObject.ListRows Property (Excel)

    PS: Might not be related, but please declare parameters explicitly with either ByRef or ByVal :

    Sub ClearFormTableRows(ByRef sourceRange As Range, ByRef countRange As Range) 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sooo basically I have a table called Comment. On that table I have three
There are sooo many posts about converting from int to string but they all
I'm sooo... close but am stuck on one tiny thing that's probably really simple
I'm sooo close... but I don't quite see the connection from the upload view
I have tried so many things but I don't understand this sooo confusing. I
I'm writing a little web application that has two things in it : At
I have been working on my dev branch for sooo long that nothing in
I am sooo stumped right now trying to get the layout that I want...I
i have a little bit lame question, but it's time i have this finally
I'm sure this is sooo simple but I've come from a c# background where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.