VBa 条件删除循环不起作用 [英] VBa conditional delete loop not working

查看:38
本文介绍了VBa 条件删除循环不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在电子表格上运行以下代码:

I am running the following code on a spreadsheet:

Do While i <= 100000
    If Not Cells(i, 4) = "String" Then
        Cells(i, 4).EntireRow.Delete
    End If
    i = i + 1
Loop

有很多不是字符串"的条目,但它们没有被删除.

There are plenty of entries with not "String" but they do not get deleted.

当我将这段代码复制到单独的工作表时,我什至收到错误Excel 无法使用可用资源完成此任务.选择较少的数据或关闭其他应用程序."

When I copy this piece of code to a separate sheet, I even get the error "Excel cannot complete this task with available resources. Choose less data or close other applications."

我做错了什么导致这个循环不起作用?

What am I doing wrong that is making this loop not work?

注意:我不能使用自动过滤器,因为我需要根据满足条件删除行.

Note: I can't use autofilter because I need to delete rows based on not meeting a condition.

推荐答案

这是一个基本的算法错误.

This is a basic algorithm mistake.

假设您的程序在第 10 行.您将其删除.因此,第 11 行变为第 10 行,第 12 行变为 11,依此类推.然后你转到第 11 行,跳过第 10 行,前第 11 行!

Imagine your program are on, say, row 10. You delete it. So, row 11 becomes row 10, row 12 becomes 11 and so on. Then you go to row 11, skipping row 10, previous row 11!

这行得通:

Do While i <= 100000
    If Not Cells(i, 4) = "String" Then
        Cells(i, 4).EntireRow.Delete
    Else
        i = i + 1
    End If
Loop

这篇关于VBa 条件删除循环不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆