vba .cells.value 应用程序定义或用户定义的错误

vba .cells.value application defined or user defined error

我正在使用以下代码计算工作表每一行中组合的出现次数:

0

1

但是 .Cells(r + 1, c).Value = activation_count 行导致运行时错误 1004:应用程序定义或用户定义的错误

谁能看出问题出在哪里?

Sub count_activations() 'to count 01 combinations in sheet

Dim row As Long
Dim col As Long
Dim r As Long
Dim c As Integer
Dim activation_count As Integer

    With Sheets("Link_TSR 10yr_Year 1_RTC1 Whitl")
    
        row = Range("A:A").Rows.Count
        col = .Cells(1, Columns.Count).End(xlToLeft).Column
        
        For c = 3 To col 
            activation_count = 0
            
                For r = 4 To row
                
                    If Cells(r, c).Value = 0 And Cells(r - 1, c).Value = 1 Then
                    activation_count = activation_count + 1
                    End If
                Next r
                    .Cells(r + 1, c).Value = activation_count
        Next c
    
    End With
    
End Sub

谢谢

抱歉,我之前忘了 post 更正后的代码。

以前,我正在计算 sheet 中的所有行,我想计算其中包含数据的所有行。干杯 Davesexcel 指出了明显的错误!

 Sub count_activations() 'to count 01 combinations in sheet

    Dim row As Long
    Dim col As Long
    Dim r As Long
    Dim c As Integer
    Dim activation_count As Integer

        With Sheets("Link_TSR 10yr_Year 1_RTC1 Whitl")
        row = .Cells(.Rows.Count, "A").End(xlUp).row 'counts only the used cells in column A
        col = .Cells(1, Columns.Count).End(xlToLeft).Column
    For c = 3 To col 
        activation_count = 0

            For r = 4 To row

                If Cells(r, c).Value = 0 And Cells(r - 1, c).Value = 1 Then
                activation_count = activation_count + 1
                End If
            Next r
                .Cells(r + 1, c).Value = activation_count
        Next c

    End With

End Sub