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

vba.cells.value应用程序定义或用户定义错误,vba,excel,runtime-error,Vba,Excel,Runtime Error,我使用以下代码计算工作表每行中组合的出现次数: 0 一, 但是,行.Cells(r+1,c).Value=activation\u 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_co

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

0

一,

但是,行
.Cells(r+1,c).Value=activation\u 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

谢谢

抱歉,我忘了早些时候发布更正后的代码

以前,我在计算工作表中的所有行,我想计算工作表中包含数据的所有行。为DaveExcel指出了明显的错误而干杯

 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

循环到工作表上的最后一行,然后尝试写入该行下方不存在的行。啊,是的,它应该是最后使用的行-明显的错误!谢谢
row=.Cells(.Rows.Count,“A”).End(xlUp)。row
也会使循环变小。