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

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

问题描述

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

  Do While i = = 100000 
如果不是单元格(i,4)=String然后
单元格(i,4).EntireRow.Delete
如果
i = i + 1
循环

有很多条目与不是String,但不会被删除。



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



我做错了什么是使这个循环不起作用?



注意:我不能使用自动过滤器,因为我需要根据不符合条件的删除行。

解决方案

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



想象你的程序是在第10行,你删除它。所以,行11成为行10,行12变为11等等。然后你去第11行,跳过第10行,上一行11!



这将工作:



<$ p $如果不是单元格(i,4)=String,那么
单元格(i,4).EntireRow.Delete
Else
i = i + 1
如果
循环


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.

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.

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!

This would work:

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天全站免登陆