自动填充function打破了一个logging

我的logging从单元格B8开始,我需要在A列中从单元格A8开始对它们进行计数。

我用这个问题的答案“ 根据相邻列自动填充 ”,但如何处理logging数为0或1时? 当有1条logging时,它是错误的。 似乎自动填充不能自动填充一个单元格。

我有一些testing零和一个条件,但有一个更干净的方法来做到这一点?

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 

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 

这有两个testing方法来完成你正在尝试做的事情。 第一种方法就是你已经使用的方法,删除阻止它工作的元素。 第二种是另一种检查方法,因为它可能会给你一个具有类似要求的未来项目的想法,但是使用自动填充不是必需的。

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:统计所有的条目,如果你只是想知道有多less总数..

 COUNTA(B8:B10000) 

编辑笔记:详细说明笔记过程,纠正拼写错误。 原始编辑添加解决scheme