VBA 错误数量的参数或无效的属性分配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33798383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:05:09  来源:igfitidea点击:

VBA Wrong number of arguments or invalid property assignment

excelvbaexcel-vba

提问by kiminem

Out of touch with vba and so i am sure its a silly mistake somewhere. Would be really helpful if someone could point that out

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

Code:

代码:

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

回答by R3uK

Firstyou had an issue of declaring your ranges, C4as itself in VBA is considered as a variable, you need to use one these :

首先你有一个声明你的范围的问题,C4因为它本身在 VBA 中被认为是一个变量,你需要使用这些:

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

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

So your lines or defining ranges should look like this :

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

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

Secondly, the .Heightproperty will give you the size of the range, not the number of rows, to get the number of rows, you need to use Range(...).Rows.Count

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

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


Here is your full code :

这是您的完整代码:

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