0

I'm a VBScript learner. I'm trying to delete the rows if a particular column's cell value is empty in excel. This is the snippet which I've written.

set mysheet1 = myxl.ActiveWorkbook.Worksheets(1) 
Row1=mysheet1.UsedRange.Rows.Count
For  i= 1 to Row1
  If len(mysheet1.cells(i,1).value) = 0 then
          mysheet1.Cells(i, 1).EntireRow.Delete
  End If
Next

Unfortunately it's not deleting the rows. What might be the issue? Please help. Thanks in advance.

1 Answer 1

3

Go backwards

Replace:

For  i= 1 to Row1

with:

For  i= Row1 to 1 Step -1

EDIT#1:

The reason that when we delete rows, we always go from bottom to top is that if we went the other way (the natural way) the loop indexing gets messed up.

Say we are going top-to-bottom and just examined and deleted row #5; we would like to examine row #6 next, but the delete changed it to row #5. This causes us to missing examining the old row #6 entirely!.

When we go from bottom-to-top, deleting rows does not affect the loop.

Sign up to request clarification or add additional context in comments.

2 Comments

please help me to understand the about change. Thanks in advance.
@arunpandiyarajhen See my EDIT#1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.