脚本不能复制来自"电子邮件" sheet to" New Sheet" - 运行时错误:对象必需的错误

时间:2014-06-08 16:31:45

标签: excel vba excel-vba

我目前正在制作一个脚本,将一些数据从一个工作表复制到另一个工作表,但我不断收到以下错误消息:

Run time error: Object required

at

Set uRng = .Range("F1", .Range("F" & .Rows.Count).End(xlUp))

可能导致什么原因?

以下代码:

Sub Collapse()
Dim uRng As Range, cel As Range
Dim comps As Variant, comp As Variant, r As Variant, v As Variant
'Dim d As Dictionary '~~> Early bind, for Late bind use commented line
Dim d As Object
Dim a As String

With Emails '~~> Sheet that contains your data
    Set uRng = .Range("F1", .Range("F" & .Rows.Count).End(xlUp))
End With

Set d = CreateObject("Scripting.Dictionary")
With d
    For Each cel In uRng
        a = Replace(cel.Offset(0, -3), "{", "}")
        comps = Split(a, "}")
        Debug.Print UBound(comps)
        For Each comp In comps
            If InStr(comp, "Computer") <> 0 _
            And Len(Trim(comp)) <= 10 Then '~~> I assumed max Comp# is 99
                If Not .Exists(cel) Then
                    .Add cel, comp
                Else
                    If IsArray(.Item(cel)) Then
                        r = .Item(cel)
                        ReDim Preserve r(UBound(r) + 1)
                        r(UBound(r)) = comp
                        .Item(cel) = r
                    Else
                        r = Array(.Item(cel), comp)
                        .Item(cel) = r
                    End If
                End If
            End If
        Next
    Next
End With

For Each v In d.Keys
    With Sheet2 '~~> sheet you want to write your data to
        If IsArray(d.Item(v)) Then
            .Range("A" & .Rows.Count).End(xlUp).Offset(1, 0) _
                .Resize(UBound(d.Item(v)) + 1) = v
            .Range("B" & .Rows.Count).End(xlUp).Offset(1, 0) _
                .Resize(UBound(d.Item(v)) + 1) = Application.Transpose(d.Item(v))
        Else
            .Range("A" & .Rows.Count).End(xlUp).Offset(1, 0) = v
            .Range("B" & .Rows.Count).End(xlUp).Offset(1, 0) = d.Item(v)
        End If
    End With
Next
Set d = Nothing

End Sub

1 个答案:

答案 0 :(得分:1)

如果您有一张名为电子邮件的工作表,则需要:

Dim Emails As Worksheet
Set Emails = Sheets("Emails")

靠近你的潜艇顶部。