如何找到包含数据的最后一列? [英] How do I find the last column with data?

查看:93
本文介绍了如何找到包含数据的最后一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了用于查找工作表中最后一个包含数据的行的方法:

I've found this method for finding the last data containing row in a sheet:

ws.Range("A65536").End(xlUp).row

是否有类似的方法来查找工作表中最后一个包含数据的列?

Is there a similar method for finding the last data containing column in a sheet?

推荐答案

很多方法可以做到这一点.最可靠的是找到.

Lots of ways to do this. The most reliable is find.

Dim rLastCell As Range

Set rLastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)

MsgBox ("The last used column is: " & rLastCell.Column)

如果要查找特定行中使用的最后一列,可以使用:

If you want to find the last column used in a particular row you can use:

Dim lColumn As Long

lColumn = ws.Cells(1, Columns.Count).End(xlToLeft).Column

使用范围(可靠性较差):

Using used range (less reliable):

Dim lColumn As Long

lColumn = ws.UsedRange.Columns.Count

如果您在A列中没有数据,则无法使用已用范围.请参见此处以了解已用范围的另一个问题:

Using used range wont work if you have no data in column A. See here for another issue with used range:

有关重置已用范围,请参见此处.

See Here regarding resetting used range.

这篇关于如何找到包含数据的最后一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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