【问题标题】:How to auto number rows if adjacent cell is not blank using VBA Excel?如果使用 VBA Excel 相邻单元格不为空白,如何自动编号行?
【发布时间】:2021-11-02 21:09:54
【问题描述】:

如果相邻单元格不为空,我需要使用 VBA 对行进行自动编号。
以下代码中的任何一个都可以完美运行,除非它与空白单元格相反。
一如既往,非常感谢您的支持。
这是预期的输出

Sub Fill_Serial_Numbers_Option1()
  Dim LastRow As Long
  LastRow = Cells(Rows.count, "B").End(xlUp).Row
  If LastRow > 2 Then
     Range("A3:A" & Application.Max(2, LastRow)) = Evaluate("ROW(A1:A" & LastRow & ")")
  End If
End Sub

Sub Fill_Serial_Numbers_Option2()
  Dim LastRow As Long
  LastRow = Cells(Rows.count, "B").End(xlUp).Row
  If LastRow > 2 Then
  With Range("A3:A" & LastRow)
    .Cells(1, 1).value = 1
    .DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1, Trend:=False
  End With
  End If
End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    请测试下一个代码:

    Sub testCountNonBlanks()
       Dim sh As Worksheet, lastR As Long, arr, arrA, count As Long, i As Long
       Set sh = ActiveSheet
       lastR = sh.Range("B" & sh.rows.count).End(xlUp).row: count = 1
       If lastR <= 2 Then Exit Sub
       arr = sh.Range("B2:B" & lastR).value 'place the range in an array for faster iteration
       arrA = sh.Range("A2:A" & lastR).value
       For i = 1 To UBound(arr)
            If arr(i, 1) <> "" Then arrA(i, 1) = count: count = count + 1
       Next i
       sh.Range("A2").Resize(UBound(arrA), 1).value = arrA
    End Sub
    

    如果允许使用公式(用 VBA 编写),则可以使用下一个变体:

    Sub testCountByFormula()
      Dim sh As Worksheet, lastR As Long, rngB As Range
      
       Set sh = ActiveSheet
       lastR = sh.Range("B" & sh.rows.count).End(xlUp).row
       Set rngB = sh.Range("B2:B" & lastR)
       sh.Range("A2:A10").Formula = "=IF(B2<>"""",COUNTA(" & rngB.Address & ")-COUNTA(" & rngB.Address(0, 1) & ")+1,"""")"
    End Sub
    

    【讨论】:

    • 第一个变体完美运行,速度更快,但另一个不行(它在单元格甚至空白单元格上插入公式)。非常感谢您的支持@FaneDuru
    • @Waleed78 很高兴我能帮上忙!但是您是否完全按照原样尝试了公式代码?如果答案是肯定的,那么 B:B 中的值是公式的结果吗?如果是,当计算返回 False 时,公式返回什么?
    • @FaneDuru.I 完全按原样尝试了第二个变体,公式没有输出(仅公式),单元格格式为通用 easyupload.io/5ffayj
    • @Waleed78 这仅表示 A:A 列被格式化为 文本...如果您之前将其格式化为 General,它将起作用。手动,或在代码中......这很奇怪,无论如何......它应该增加公式第二部分中 B2 的值。可能也是因为格式。
    • @Waleed78 所以,你没有测试代码原样... :)
    【解决方案2】:

    您不需要宏来完成此操作。假设您关心的所有内容是否为空白,那么您可以在单元格A9 中使用这样的公式。 =Counta($B$1:$B9)如果你有公式,你可以尝试利用 COuntif 来利用一些东西。

    【讨论】:

    • @pgSystemTester.I 知道公式,但由于某些原因,我不需要 coulma A 上的公式。
    • 那么请使用 FaneDuru 的答案。创建一个数组是要走的路。
    【解决方案3】:

    您可以使用从第一行到最后一行的循环,如下所示:

    Sub Fill()
        Dim LastRow As Long
        Dim Count As Integer
        Dim Row As Integer
        
        Count = 0
        Row = 1
        LastRow = Cells(Rows.Count, "B").End(xlUp).Row
        
        
        Do While Row <= LastRow
            If Not (Cells(Row, 2) = "") Then
                Count = Count + 1
                Cells(Row, 1) = Count
            End If
            Row = Row + 1
            
        Loop
    
    End Sub
    

    【讨论】:

    • 你的代码从标题行开始填充(A1和A2合并在一个单元格中),所以结果不正确@Adrian Fischer
    • 我将 Row=1 更改为 Row=2,现在可以正常工作了。
    • 是的,没错。当我测试它时,我没有标题。
    猜你喜欢
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多