【问题标题】:VBA Wrong number of arguments or invalid property assignmentVBA 参数数量错误或属性分配无效
【发布时间】:2015-11-19 16:34:22
【问题描述】:

与 vba 脱节,所以我确信它在某个地方是一个愚蠢的错误。如果有人能指出这一点会非常有帮助

代码:

Private Function generate() As Integer

Dim source_size As Long
Dim target_size As Long
Dim i As Long
Dim j As Long
Dim count As Long
Dim source1 As Range
Dim target1 As Range

Set source1 = Worksheets("Filter").Range(C4, C6498)
Set target1 = Worksheets("30").Range(A2, AP95787)


source_size = source1.Height
target_size = target1.Height

For i = 1 To source_size Step 1
    For j = 1 To target_size Step 1
        If Application.source1.Cells(i, 1).Value = target1.Cells(j, 5).Value Then
            target1.Row(j).Select
            'Selection.Copy
            Worksheet("result").Range("A1").Rows("1:1").Insert Shift:=xlDown
        End If
    Next j
Next i
generate = 0


End Function

【问题讨论】:

  • 你从哪里得到错误?该代码的期望行为是什么?在编辑您的帖子以改进它之前,请花点时间阅读此链接:stackoverflow.com/help/how-to-ask
  • 请参阅stackoverflow.com/questions/2237873/… 了解什么是 .Height 属性
  • 可能Range(d, C6498) 以这种方式无效。你可以使用例如Range("A1:C3")Range(Cells(...), Cells(...))。也许你想要Range("C4:C6498")

标签: vba excel


【解决方案1】:

首先您在声明范围时遇到了问题,C4 在 VBA 中被视为变量,您需要使用这些:

[C4]Range("C4")Cells(4,3)Cells(4,"C")

所以你的行或定义范围应该是这样的:

Set source1 = Worksheets("Filter").Range([C4], [C6498])
Set target1 = Worksheets("30").Range(Range("A2"), Range("AP95787"))

其次.Height属性会给你范围的大小,而不是行数,要获取行数,你需要使用Range(...).Rows.Count

source_size = source1.Rows.count
target_size = target1.Rows.count

这是你的完整代码:

Option Explicit
Public Function generate() As Integer
Dim source_size As Long
Dim target_size As Long
Dim i As Long
Dim j As Long
Dim count As Long
Dim source1 As Range
Dim target1 As Range

Set source1 = Worksheets("Filter").Range("C4:C6498")
Set target1 = Worksheets("30").Range("A2:AP95787")
source_size = source1.Rows.count
target_size = target1.Rows.count

For i = 1 To source_size Step 1
    For j = 1 To target_size Step 1
        If Application.source1.Cells(i, 1).Value = target1.Cells(j, 5).Value Then
            target1.Rows(j).Select
            'Selection.Copy
            Worksheets("result").Range("A1").Rows("1:1").Insert Shift:=xlDown
        End If
    Next j
Next i
generate = 0
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多