Excel 动态数据范围中的自动填充增量数

Excel 动态数据范围中的自动填充增量数,excel,vba,Excel,Vba,我卡在我的vba代码,似乎我设置了一个循环错误。非常感谢你的建议!非常感谢 Sub code() Dim lastRow As Long Dim k As Integer Dim rowPtr As Long lastRow = Cells(Rows.Count, 1).End(xlUp).Row For rowPtr = 2 To lastRow If Range("A" & rowPtr + 1) <> Range("A&qu

我卡在我的vba代码,似乎我设置了一个循环错误。非常感谢你的建议!非常感谢

Sub code()

Dim lastRow As Long
Dim k As Integer
Dim rowPtr As Long

lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For rowPtr = 2 To lastRow
     
If Range("A" & rowPtr + 1) <> Range("A" & rowPtr) Then
k = 1
Range("B" & rowPtr) = k

Else
        
If Range("A" & rowPtr + 1) = Range("A" & rowPtr) Then
Range("B" & rowPtr) = k
             
End If
k = k + 1
End If

Next
End Sub
上面是我的代码,现在我的VBA结果如下:


C列是我理想的代码结果

请尝试下一种方法:

Sub Countcode()
 Dim lastRow As Long, k As Long, rowPtr As Long

 lastRow = cells(rows.count, 1).End(xlUp).row
 k = 1
 For rowPtr = 2 To lastRow
    If Range("A" & rowPtr) = Range("A" & rowPtr + 1) Then
        Range("B" & rowPtr) = k: k = k + 1
    Else
        If Range("A" & rowPtr) = Range("A" & rowPtr - 1) Then
            Range("B" & rowPtr) = k: k=1
        Else
            k = 1
            Range("B" & rowPtr) = k
        End If
    End If
 Next
End Sub
对重复的值进行排序 调整常量部分中的值。 请注意,RangeA&rowPtr与CellsrowPtr、A或CellsrowPtr、1相同,RangeA&Rows.Count与CellsRows.Count、A或CellsRows.Count、1相同。 选项显式 地下铁路 常量第一行长度=2 Const sCol As String=A 常量dCol作为字符串=B 将cOffset设置为长度:cOffset=ColumnsColumn.Column-ColumnsColumn.Column 将LastRow的长度调整为:LastRow=RangesCol&Rows.Count.EndxlUp.Row 如果LastRowFirstRow,则 将cCell变暗为“范围”当前单元格 变暗r为长行计数器 尺寸rk为长:rk=1'列计数器 对于r=FirstRow+1到LastRow'+1:第一个已写入 设置cCell=RangesCol&r 如果cCell.Value=cCell.Offset-1.Value,则 rk=rk+1 其他的 rk=1 如果结束 cCell.Offset,cOffset.Value=rk 下一个r 如果结束 端接头 一种方法是:

Sub numberIt2()
    Dim cl As Range, equal As Integer ' equal initial value is 0
    
    Set cl = Range("A1")
    Do While cl <> ""
        cl.Offset(0, 1) = equal + 1
        Set cl = cl.Offset(1)
        equal = IIf(cl = cl.Offset(-1), equal + 1, 0)
    Loop
End Sub

旁注:为什么VBA不是一个简单的公式?因为这个文件是供别人使用的,我想用VBA来生成,而不是有其他代码在执行结果,我只是把它的一部分真的很感激!!非常精确和清晰,非常感谢!非常感谢:这适用于我文件的其他部分,并帮助我开发其他功能,谢谢!我喜欢应用偏移和检查最后一列/行的方式,非常感谢!!
Public Sub UpdateRankings(ByVal ws As Worksheet)

    ' Adjust as necessary.
    Const firstRow As Long = 3
    Const colGroupId As Long = 1
    Const colRanking As Long = 6
    
    Dim row As Long
    
    With ws
    
        ' First value defaults to 1.
        row = firstRow
        .Cells(row, colRanking).Value = 1

        ' Remaining rows.
        row = row + 1

        Do While .Cells(row, colGroupId).Value <> ""

            ' If group id is the same as the previous row, increment rank.
            If .Cells(row, colGroupId).Value = .Cells(row - 1, colGroupId).Value Then
                .Cells(row, colRanking).Value = .Cells(row - 1, colRanking).Value + 1

            ' If group id has changed, reset rank to 1.
            Else
                .Cells(row, colRanking).Value = 1

            End If

            ' Next row.
            row = row + 1

        Loop
        
    End With
    
End Sub