簡體   English   中英

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

謝謝

抱歉,我忘記了更早地發布更正后的代碼。

以前,我是在計算工作表中的所有行,而我要計算其中包含數據的所有行。Cheers 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM