vba 使用宏查找excel工作表中使用的列数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21455218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find number of columns used in a excel worksheet using macro
提问by Gauranga
I am working on excel sheets using macros
.
我正在使用macros
.
I wanted to count the number of columns used in the sheet.
I used:ColLen = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Column
on a particular workbook and it gave a value one greater than the used number of columns.
On other normal excel workbooks, it is giving the correct value.
Another command is giving the correct value for number of columns, which is:ColLen = Activeworksheet.Cells(1, Columns.count).End(xlToLeft).Column
我想计算工作表中使用的列数。我使用了:ColLen = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Column
在一个特定的工作簿上,它给出的值比使用的列数大一。在其他普通的 excel 工作簿上,它给出了正确的值。
另一个命令是为列数提供正确的值,即:ColLen = Activeworksheet.Cells(1, Columns.count).End(xlToLeft).Column
Its other part i.e. number of rows used giving correct value in all sheets.
RowLen = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.count).Row
Any clue what can be wrong in that sheet? I can't explain its details as told by my company. So, it may be difficult to answer as it may be illogical to ask. If you have any idea, please tell.
它的另一部分即使用的行数在所有工作表中给出正确的值。
RowLen = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.count).Row
知道那张纸有什么问题吗?我无法解释我公司所说的细节。因此,可能很难回答,因为提问可能不合逻辑。如果您有任何想法,请告诉。
回答by Gauranga
To get columns, the most reliable code is: (it works under all cases)
要获取列,最可靠的代码是:(它适用于所有情况)
Set rng = ws.Cells.Find(What:="*", _
After:=ws.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
If rng Is Nothing Then
LastCol = 1
Else
LastCol = rng.Column
End If
`
`