VBA在Excel上删除给定范围内的空白行 [英] VBA Delete blank rows in a given range on Excel

查看:76
本文介绍了VBA在Excel上删除给定范围内的空白行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在考虑将其标记为重复问题之前,请让我向您保证,我找不到满足该非常简单需求的答案.所以我附有一张图片,可以帮助我准确描述我想做什么.请向下滚动并传递图片.

Before thinking to flag this as a duplicate question, please let me assure you that I found not an answer satisfying this very simple need. So I have a picture included to help me describe what EXACTLY I want to do. Please scroll down and pass the picture.

想象一下,如何以整洁的方式设置数据.第1至9行必须保持原状.第22行等必须保持不变.

Imagine data being set in a neat way. Rows 1 to 9 must stay untouched. Rows 22 and so on must stay untouched.

从第21行到第10行(按预期的方式),我希望代码检查该行是否为空白.如果该行为空白,我希望删除该行.完全删除.否则,我希望它保持不变.

From line 21 to 10 (that way intended) I want the code to check if the line is blank. If the line is blank I want the line deleted. Removed entirely. Otherwise, I want it untouched.

因此,在图片中,第1至9行保持不变.从21到10行,只有10和11停留.第22行等保持不变.

Thus in the picture, lines 1 to 9 stay untouched. From lines 21 to 10, only 10 and 11 stay. Lines 22 and so on stay untouched.

检查第一个单元格是否为空将解决问题,我只希望正确的语法使其正确执行,并且仅在示例中从A21到A10.

Checking the first cell if it is null, will do the trick, I just want the correct syntax to make it happen right and only within A21 to A10 for the example.

这可能非常简单,但是由于某种原因,由于我是VBA方法的新手,因此这给我带来了艰难的时机.

It is probably stupidly easy, but for some reason it gives me a hard time to do it right, since I am new to the VBA methods.

我肯定会有某种涉及的 EntireRow.Delete 方法我对使用VBA所做的一切还不太满意.

I am sure there will be the EntireRow.Delete method involved somehow I am just not so good yet with what you can do with VBA yet.

这个问题的第一个答案很接近我的需求,但到目前为止.

The first answer to this question was pretty near my needs and yet so far.

Excel VBA删除范围内的空白行

问题是我不是很有经验,我也不了解所有方法,也没有将所有参数都传递给该示例.这有点复杂,我希望看到的是简单的东西,以了解发生了什么的方式记录在案,因此我可以在现实生活中实现它.

Problem is that I am not very experienced and I don't understand all the methods, neither all arguments passed to that example. It is kinda complex and what I would love to see is something simple, documented in a way to understand what is going on, so I can implement it on my real life scenario.

这个问题: Excel VBA:删除整行,如果A列中的单元格为空(长数据集)

这个问题有整个电子表格的答案,对我来说真的没有帮助.

The question has an answer for the ENTIRE spreadsheet, not helpful really in my case.

正如您所看到的,我提供一个示例,我不是要您做我的工作,我只是想坦诚地想学习如何做我所要求的事情.

As you can see, I provide an example, I am not asking you to do my job, I just honestly wanna learn how to do what I asked.

我不确定是否需要用否定的步骤-1进行迭代的FOR(包括循环内的某些方法),或者是否有现成的方法(包括循环),所以我在这里问更多有经验的人如何做某事,可能非常简单.
任何帮助将不胜感激.

I am not sure if I need a FOR that iterates with negative step -1, including some methods inside the loop, or if there is a ready method for this, including a looping, so here I am asking more experienced people how to do something, probably ridiculously easy.
Any help would be appreciated.

推荐答案

您需要从范围的结尾向后循环,并删除相关的行索引,就像这样

You need to loop backwards from the end of the range and delete the relevant row index, like so

for x=numrows to 1 step -1
    if ws.range("a" & x).value="" then ws.rows(x).delete
next x 

您正在处理的工作表在哪里.

Where ws is the worksheet you are working on.

这篇关于VBA在Excel上删除给定范围内的空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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