如何使用VBA查找Excel工作表中的已用列数?
Dim lastRow As Long
lastRow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
MsgBox lastRow
使用上面的VBA,我能够找到行数。但是我如何在给定的excel文件中找到列数呢?
发布于 2011-08-01 19:42:36
您的示例代码获取当前列中最后一个非空单元格的行号,可以按如下方式重写:
Dim lastRow As Long
lastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
MsgBox lastRow
然后很容易看出,用于获取当前行中最后一个非空单元格的列号的等效代码为:
Dim lastColumn As Long
lastColumn = Sheet1.Cells(1, Columns.Count).End(xlToLeft).Column
MsgBox lastColumn
这也可能对你有用:
With Sheet1.UsedRange
MsgBox .Rows.Count & " rows and " & .Columns.Count & " columns"
End With
但请注意,如果A列和/或第1行为空,则这将不会产生与上面其他示例相同的结果。有关更多信息,请阅读UsedRange
属性。
发布于 2013-12-20 09:58:06
您可能每次都在columns.count
之前的某个地方忘记了一个sheet1
,或者它将计算activesheet
列而不是sheet1
列。
另外,它不应该是xltoleft而不是xltoright吗?(好的,这里很晚了,但我想我知道我的右边和左边)我检查过了,你必须写xltoleft。
lastColumn = Sheet1.Cells(1, sheet1.Columns.Count).End(xlToleft).Column
发布于 2017-08-04 08:48:02
https://stackoverflow.com/questions/6897347
复制相似问题