我的记录从单元格B8开始,我需要在A列中计算它们,从单元格A8开始。
我使用了这个问题的答案" autofill down according to adjacent column"但是当recordcount为0或1时我该如何处理?当有1条记录时,它会出错。似乎自动填充无法自动填充一个单元格。
我有一些条件测试零和一,但是有更清洁的方法吗?
LastRow = Cells(Rows.Count, 2).End(xlUp).Row 'Find last row
If LastRow >= 8 Then 'If recordcount is 1 or greater
Range("A8").FormulaR1C1 = "1" 'First number is "1"
If Not LastRow = 8 Then 'If not recordcount exactly 1
'Enumerate the remaining records with Autofill
Range("A8").AutoFill Destination:=Range("A8:A" & LastRow), Type:=xlLinearTrend
End If
End If
答案 0 :(得分:1)
这有两种经过测试的方法来完成你想要做的事情。第一种方法是您已经使用的方法,删除阻止其工作的元素。第二种方法是另一种检查方法,因为它可能会为您提供具有类似要求的未来项目的想法,但使用自动填充并不是必需的。
<强> TESTED 强>
Sub AutoFiller()
Dim LastRow As Long
LastRow = Cells(Rows.Count, "B").End(xlUp).row 'Find last row
Sheets("Sheet1").Cells(8, 1) = 1 'First number is "1"
Range("A8").AutoFill Destination:=Range("A8:A" & LastRow), Type:=xlLinearTrend
End Sub
使用LOOP代替自动填充:
Sub VBAAutoFill()
Dim LastRow As Long
Dim count As Long
LastRow = Cells(Rows.Count, 2).End(xlUp).Row
count = 1
For lRow = 8 to LastRow
Sheets("Sheet1").Cells(lRow, 1) = count
count = count + 1
Next lRow
end Sub
EXCEL FUNCTION:计算您的所有参赛作品,如果您只想知道总数是多少..
COUNTA(B8:B10000)
编辑笔记:详细说明笔记中的过程,纠正错别字。原始编辑添加了解决方案。
答案 1 :(得分:1)
My records start in cell B8, and I need to count them in Column A, starting in cell A8.
我认为这意味着,你实际上是在枚举行。这有助于解决您的错误问题,如果它不完全符合您在A
列中的要求,则您不应该很难进行调整:
Public Sub GetRangeCount()
Dim bRange As Range
Set bRange = Range(Range("B8"), Range("B1048575").End(xlUp))
Dim i As Integer
i = 0
For Each c In bRange
If c.Value <> "" Then
i = i + 1
c.Offset(0, -1).Value = i
End If
Next
End Sub