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

  • Home
  • SEARCH
  • 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 9035761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:49:59+00:00 2026-06-16T08:49:59+00:00

I parsed through an Excel spreadsheet and returned the entire result as a DataTable.

  • 0

I parsed through an Excel spreadsheet and returned the entire result as a DataTable. However, this Excel spreadsheet has several empty rows that I would like to eliminate from the resulting DataTable. In particular, each empty row begins with an empty cell. So, I know that the entire row is empty if the value of the cell at the first index is empty. Note that I cannot simply modify the Excel spreadsheet because I have to work with exactly what the client has sent to me. Based on this information, I assumed that I could perform the following function to remove empty rows:

' DataTable dt has already been populated with the data

For Each row As DataRow In dt.Rows
    If dt.Rows.Item(0).ToString = "" Then
        dt.Rows.Remove(row)
    ElseIf dt.Rows.Item(0) Is Nothing Then
        dt.Rows.Remove(row)
    End If
Next

However, after crafting this solution, I am greeted with the following error:

Collection was modified; enumeration operation might not execute.

I now realize that I cannot alter the collection as I access it. How can I get around this? I’m wondering if I should create a new DataTable with the rows that aren’t empty. Is this the best approach or are there better options?

EDIT: I have also tried iterating over the rows backwards:

For i = dt.Rows.Count() - 1 To 0
    If dt.Rows.Item(i).Item(0).ToString = "" Then
        dt.Rows.RemoveAt(i)
    End If
Next
  • 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-16T08:50:00+00:00Added an answer on June 16, 2026 at 8:50 am

    You can’t modify the collection while you’re enumerating it with For Each, but a simple For loop will work. You’ll need to loop backwards to avoid skipping the row after a removed row.

    You’ve also got your tests the wrong way round; if Item(0) returns Nothing, then Item(0).ToString will throw a NullReferenceException.

    I’m assuming the dt.Rows.Item(0) is a typo, and should read row.Item(0) instead.

    For i As Integer = dt.Rows.Count - 1 To 0 Step -1
        Dim row As DataRow = dt.Rows(i)
        If row.Item(0) Is Nothing Then
           dt.Rows.Remove(row)
        ElseIf row.Item(0).ToString = "" Then
           dt.Rows.Remove(row)
        End If
    Next
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My understanding is that indexing a PDF, Word, Excel, etc. document through Solr will
I'd like to be able to run a script that parsed through the twitter
When I create excel sheet through java ,the column which has number datatype in
I'm trying handle bad json data when parsed through json_decode(). I'm using the following
Are there any automated tools that will parse through XAML code and clean out
I work at a site that passed through some face lifts. In time the
I have a set of complex Excel files (with figures in it) that I
I've had some code that has worked for years to export an html table
So basically I have this field in XML that I want to update. It
The problem: Loading an excel spreadsheet template. Using the Save command with a different

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.