Excel VBA代码以选择非空单元格 [英] Excel VBA code to select non empty cells

查看:898
本文介绍了Excel VBA代码以选择非空单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Excel中,让我们在B2至B7和C2至C7中存储数据.在VBA中,我可以编写一个宏来选择它:

In Excel, let's I have data in B2 to B7 and C2 to C7 . In VBA I can write a macro to select it:

Sub Macro1()
Range("B2:C7").Select
End Sub

如何重写代码,以便它自动选择非空单元格? 如果我删除单元格B7和C7中的数据,那么我希望宏仅选择Range(B2:C6) 而且,如果我将数据添加到单元格B8和C8中,那么我希望宏选择Range(B2:C8).

How do I rewrite the code so that it chooses automatically the cells that are non-empty? If I delete the data in cell B7 and C7 then I want the macro to select only Range(B2:C6) And if I add data to Cell B8 and C8 then I want the macro to choose Range(B2:C8).

我的数据将始终以B2,C2开头,并且数据之间将没有任何可用空间.

My data will always start a B2,C2 and I will not have any free space between data.

推荐答案

您的数据总是从B2,C2开始并且之间没有空单元格吗?如果是这样,您可以将变量设置为最后填写的行"

your data always start at B2,C2 and has no empty cell inbetween? If so you can set a variable to be the "last filled in row"

lastRow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
Range("B2:C" & lastRow).Select

并定义从B2到C最后一行"的范围

and define the range from B2 to the C"last row"

这篇关于Excel VBA代码以选择非空单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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