运行时错误1004无法获取工作表的countif属性

时间:2014-08-19 07:20:31

标签: excel vba countif

以下是我的代码:

Sub yenilikleri_ekle()

    Dim sh1 As Worksheet, sh2 As Worksheet, lr As Long, rng As Range
    Set sh1 = ThisWorkbook.Worksheets("Input")
    Set sh2 = ThisWorkbook.Worksheets("Ayarlar")
    lr = sh1.Cells(Rows.Count, 2).End(xlUp).Row
    Set rng = sh1.Range("B5:B" & lr)
    For Each c In rng
        If WorksheetFunction.CountIf(sh2.Range("A:A"), c.Value) = 0 Then
            sh2.Range("A" & sh2.Cells(Rows.Count, 1).End(xlUp).Row + 1) = c.Value

        End If
    Next
End Sub

它的作用是匹配sh1sh2中的单元格。如果它不在sh2,则会将Cell.Value添加到sh2。代码工作正常,直到单元格有超过255个字符的文本。 (直到它是一篇长篇文章)

当文字很长时,它会返回错误"Run-time error 1004: Unable to get countif property of the worksheet",这对我来说是无意义的。我无法解决问题2天,需要你的帮助。我为Dim添加了c但没有更改,仍然是同样的错误。我改为CountA,但后来没有反应。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如果目标是让ThisWorkbook.Worksheets("Input").Range("B:B") ThisWorkbook.Worksheets("Ayarlar").Range("A:A")没有重复且ThisWorkbook.Worksheets("Ayarlar").Range("A:A")之前为空,那么这应该是有效的:

With ThisWorkbook
    .Worksheets("Input").Columns("B:B").Copy Destination:=.Worksheets("Ayarlar").Range("A1")
    .Worksheets("Ayarlar").Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo
End With

问候

阿克塞尔