如果某些单元格为空白,如何计算行中使用过的单元格
Sub atest()
Dim LR As Long, i As Long
Dim LC As Long, s As Long
Dim myrange As String
LR = Range("B" & Rows.Count).End(xlUp).Row
LC = Range((LR) & Column.Count).End(xlToLeft).Column
答案 0 :(得分:2)
只需使用原生的COUNTA()函数,您就可以通过WorksheetFunction
类
cellCount = WorksheetFunction.CountA(LR.EntireRow)
当已经有一个完美的功能来实现这一点时,没有必要重新发明轮子。
答案 1 :(得分:0)
不是从数据末尾开始获取最大列数,而是使用Cells()
Sub atest()
Dim LR As Long, _
i As Long, _
LC As Long, _
s As Long, _
MyRange As String
With Sheets("Sheet1")
LR = .Rows(.Rows.Count).End(xlUp).Row
'Or
'LR = .Range("B" & Rows.Count).End(xlUp).Row
LC = .Range(.Cells(1, Columns.Count)).End(xlToLeft).Column
'Or
'LC = .Range(.Cells(LR,Columns.Count)).End(xlToLeft).Column
End With
End Sub
答案 2 :(得分:0)
如果某些单元格为空白,如何计算行中使用过的单元格
这应该仅计算使用过的单元格
Sub CountUsedCells()
Dim CountUsedCells As Long
CountUsedCells = Worksheets("Sheet1").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).count
MsgBox " Total used cells are " & CountUsedCells
End Sub