使用Set但错误'应用程序定义的或对象定义的错误'出现

时间:2016-07-06 08:38:55

标签: excel-vba vba excel

对不起,问题没有明确说明。我尝试使用Set来帮助我填写一个质量控制表,但错误10o4出现了。这似乎对我来说是正确的,所以我不知道如何纠正它。所以请看一下,如果您有所了解,请发表评论。谢谢!

Private Sub FillTarget(ByVal TargetSheet As String, ByVal DepositSheet As String, _
        ByVal TargetRow As Integer, ByVal TargetColumn As String, _
        ByVal DepositRow As Integer, ByVal DepositColumn As Integer)

Dim i, j, k As Integer
Dim OpenedFile As String
Dim myObject As String

Dim MarkRow As Integer
MarkRow = 1
Dim myData As String
Dim EndSearch As Boolean

Dim TargetCell As Range
Dim TargetTag As Range
Dim TargetType As Range
Dim TargetZone As Range
Dim TargetTest As Range

Dim DepositTag As Range
Dim DepositZone As Range
Dim DepositResult As Range

For i = 3 To TargetRow
    With Worksheets(TargetSheet)
        myObject = .Cells(i, 15).Text + "_" + Worksheets(TargetSheet).Cells(i, 17).Text
        Set TargetCell = .Cells(i, TargetColumn) <==== Here comes the error
        Set TargetTag = .Cells(i, 15)
        Set TargetType = .Cells(i, 17)
        Set TargetZone = .Cells(i, 18)
        Set TargetTest = .Cells(i, 20)
    End With

    For j = MarkRow To DepositRow
        With Worksheets(DepositSheet)
            Set DepositTag = .Cells(j, 1)
            Set DepositZone = .Cells(j, 2)
        End With

        If InStr(DepositTag.Text, myObject) <> 0 Then
            OpenedFile = OpenedFile & DepositTag.Text & "|"

            If InStr(DepositZone.Text, TargetZone.Text + ":") <> 0 _
                Or InStr(TargetZone.Text, "/") <> 0 Then

                For k = 2 To DepositColumn
                    With Worksheets(DepositSheet)
                        Set DepositResult = .Cells(j, k)
                    End With

                    If InStr(DepositResult.Text, TargetTest.Text) <> 0 Then
                        MarkRow = j
                        myData = DepositResult.Text
                        'Split_monData
                        'Derniere_Colonne
                        TargetCell.Value = myData
                        EndSearch = True
                        Exit For
                    End If

                Next k

            End If

        End If

        If EndSearch Then Exit For
    Next j

    EndSearch = False
Next i
End Sub

1 个答案:

答案 0 :(得分:1)

Cells()的默认值是单元格的值。将其作为Range传递以定义Range对象变量。

在Range变量等于.Cells(something)的任何地方,将其更改为.Range(.Cells(something).Address)并且它应该按预期工作

此外,TargetColumn被声明为字符串,而不是数值。如果字符串值不是列名(即“A”或“B”),则会出错。使用Cdbl()CInt()或Clng()将变量传递给数值,或者从开头用数字类型声明变量