ERROR对象变量或块变量未设置....究竟是什么意思

时间:2016-10-21 06:56:26

标签: vba macros ribbon excel-addins

我添加我的宏代码以添加和自定义功能区,现在它显示错误“对象变量或未设置块变量”... enter image description here

当我第二次运行我的添加宏代码时,会发生此错误 enter image description here

应该设置什么类型的对象变量或声明任何帮助?

代码:      选项明确

 Sub MakeWordList()
 Dim answer As VbMsgBoxResult

  answer = MsgBox("ARE YOU SURE YOU WANT TO RUN THIS WORD COUNT MACRO...? ",       vbYesNo, "RUN WORD COUNT")
If answer = vbYes Then

Dim InputSheet As Worksheet
Dim WordListSheet As Worksheet
Dim Result As Worksheet
Dim PuncChars As Variant, x As Variant
Dim i As Long, r As Long, lastrow As Long
Dim txt As String
Dim wordCnt As Long
Dim AllWords As Range
Dim pc As PivotCache
Dim pt As PivotTable
Dim OutClm As Long

Application.ScreenUpdating = False
Set InputSheet = ActiveSheet
Set WordListSheet = Worksheets.Add(After:=Worksheets(Sheets.Count))
WordListSheet.Activate
WordListSheet.Name = "temp"
WordListSheet.Range("A1:B1") = "All Words"
WordListSheet.Range("A1:B1").Font.Bold = True
InputSheet.Activate
wordCnt = 2
PuncChars = Array(".", ",", ";", ":", "'", "!", "#", "-", "--", "---", "@", "`", _
   "$", "%", "&", "(", ")", " - ", "_", "--", "+", "<", ">", "BLANK", _
    "=", "~", "/", "\", "{", "}", "[", "]", """", "?", "*", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
        r = 1

   OutClm = 1
 '   Loop until blank cell is encountered
     Do While Cells(r, 1) <> ""
 '       covert to UPPERCASE
         txt = UCase(Cells(r, 1))
  '       Remove punctuation
       For i = 0 To UBound(PuncChars)
              txt = Replace(txt, PuncChars(i), "")
        Next i
     '       Remove excess spaces
       txt = WorksheetFunction.Trim(txt)
   '       Extract the words
    x = Split(txt)

    For i = 0 To UBound(x)
        If wordCnt > WordListSheet.Rows.Count Then
             OutClm = OutClm + 1
             wordCnt = 1
         End If
            WordListSheet.Cells(wordCnt, OutClm) = x(i)
            wordCnt = wordCnt + 1
    Next i
r = r + 1
Loop
'
  ' Macro1 Macro
  WordListSheet.Activate

Columns("A:A").Select
Selection.Copy
Columns("D:D").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Range("E1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "COUNT"
Range("D2").Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
Range("E2").Select
ActiveCell.FormulaR1C1 = "1"
Range("D3").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Range("E1048576").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Range("E1048575").Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Columns("D:E").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "temp!R1C4:R1048576C5", Version:=xlPivotTableVersion15).CreatePivotTable _
    TableDestination:="temp!R1C7:R1C8", TableName:="PivotTable1", _
    DefaultVersion:=xlPivotTableVersion15
Sheets("temp").Select
Cells(1, 7).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("All Words")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("COUNT"), "Sum of COUNT", xlSum
Columns("G:H").Select
Selection.Copy
Columns("J:K").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Columns("B:B").Select
Application.CutCopyMode = False
Selection.Copy
Columns("M:M").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Range("M1").Select
Application.CutCopyMode = False
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.FormulaR1C1 = "ALL WORDS"
Range("N1").Select
ActiveCell.FormulaR1C1 = "COUNT"
Range("N2").Select
ActiveCell.FormulaR1C1 = "1"
Range("M3").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Range("N1048576").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Range("N1048575").Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Columns("M:N").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "temp!R1C13:R1048576C14",      Version:=xlPivotTableVersion15).CreatePivotTable _
    TableDestination:="temp!R1C16:R1C17", TableName:="PivotTable2", _
    DefaultVersion:=xlPivotTableVersion15
Sheets("temp").Select
Cells(1, 16).Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("ALL WORDS")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
    "PivotTable2").PivotFields("COUNT"), "Sum of COUNT", xlSum
Selection.End(xlUp).Select
Selection.End(xlToRight).Select
Columns("P:Q").Select
Selection.Copy
Columns("S:T").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Range("S2:T2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Selection.End(xlUp).Select
Selection.End(xlToLeft).Select
Selection.End(xlUp).Select
Selection.End(xlToLeft).Select
Selection.End(xlUp).Select
Range("J3").Select
Selection.End(xlDown).Select
Range("J113487:K113487").Select
ActiveSheet.Paste
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
Range("J226972:K226972").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("J226970").Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Range("J1:K1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.End(xlUp).Select
Range("J2").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
Range("J226971:K226971").Select
Range(Selection, Selection.End(xlUp)).Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "temp!R1C10:R226971C11", Version:=xlPivotTableVersion15).CreatePivotTable _
    TableDestination:="temp!R1C22:R1C23", TableName:="PivotTable3", _
    DefaultVersion:=xlPivotTableVersion15
Sheets("temp").Select
Cells(1, 22).Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Row Labels")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
    "PivotTable3").PivotFields("Sum of COUNT"), "Sum of Sum of COUNT", xlSum
ActiveWindow.ScrollColumn = 9
ActiveWindow.ScrollColumn = 10
ActiveWindow.ScrollColumn = 11
ActiveWindow.ScrollColumn = 12
ActiveWindow.ScrollColumn = 13
ActiveWindow.ScrollColumn = 14
ActiveWindow.ScrollColumn = 15
ActiveWindow.ScrollColumn = 16
Range("W2").Select
ActiveSheet.PivotTables("PivotTable3").PivotFields("Row Labels").AutoSort _
    xlDescending, "Sum of Sum of COUNT", ActiveSheet.PivotTables("PivotTable3"). _
    PivotColumnAxis.PivotLines(1), 1
Range("V1:W1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range("V1:W1048573").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste

End If

 End Sub

2 个答案:

答案 0 :(得分:0)

以下内容远非完美重构您的代码,但它应该帮助您朝着正确的方向避免Activate / Select模式

你的代码的最后一部分被评论,因为它没有运行,因为很可能缺少一些代码

逐步完成并查看需要修改的内容

良好的编码

Option Explicit

Sub MakeWordList()
    Dim answer As VbMsgBoxResult

    answer = MsgBox("ARE YOU SURE YOU WANT TO RUN THIS WORD COUNT MACRO...? ", vbYesNo, "RUN WORD COUNT")
    If answer = vbYes Then

        Dim InputSheet As Worksheet, WordListSheet As Worksheet
'        Dim Result As Worksheet '<-- not used
        Dim PuncChars As Variant, x As Variant
        Dim i As Long, r As Long, lastrow As Long
        Dim txt As String
        Dim wordCnt As Long
'        Dim AllWords As Range'<-- not used
'        Dim pc As PivotCache '<-- not used
'        Dim pt As PivotTable '<-- not used
        Dim OutClm As Long

'        Application.ScreenUpdating = False
        Set InputSheet = ActiveSheet

        Set WordListSheet = Worksheets.Add(After:=Worksheets(Sheets.Count))
        With WordListSheet
            .name = "temp"
            With .Range("A1:B1")
                .Value = "All Words"
                .Font.Bold = True
            End With
        End With

        wordCnt = 2
        PuncChars = Array(".", ",", ";", ":", "'", "!", "#", "-", "--", "---", "@", "`", _
        "$", "%", "&", "(", ")", " - ", "_", "--", "+", "<", ">", "BLANK", _
        "=", "~", "/", "\", "{", "}", "[", "]", """", "?", "*", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
        r = 1
        OutClm = 1
        With InputSheet
        '   Loop until blank cell is encountered
            Do While .Cells(r, 1) <> ""
                '       covert to UPPERCASE
                txt = UCase(.Cells(r, 1))
                '       Remove punctuation
                For i = 0 To UBound(PuncChars)
                     txt = Replace(txt, PuncChars(i), "")
                Next i
                '       Remove excess spaces
                txt = WorksheetFunction.Trim(txt)
                '       Extract the words
                x = Split(txt)

                For i = 0 To UBound(x)
                    If wordCnt > WordListSheet.Rows.Count Then
                        OutClm = OutClm + 1
                        wordCnt = 1
                    End If
                    WordListSheet.Cells(wordCnt, OutClm) = x(i)
                    wordCnt = wordCnt + 1
                Next i
                r = r + 1
            Loop
        End With
        '
        ' Macro1 Macro
        With WordListSheet
            .Columns("A:A").Copy
            .Columns("D:D").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            Application.CutCopyMode = False
            .Range("E1").FormulaR1C1 = "COUNT"
            .Range("E2").FormulaR1C1 = "1"
            .Range(.Range("E1048576"), .Range("E1048576").End(xlUp)).FillDown
            .Columns("D:E").Select
            ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
                                              SourceData:="temp!R1C4:R1048576C5", _
                                              Version:=xlPivotTableVersion15) _
                                      .CreatePivotTable TableDestination:="temp!R1C7:R1C8", _
                                                        TableName:="PivotTable1", _
                                                        DefaultVersion:=xlPivotTableVersion15

            .Cells(1, 7).Select
            With .PivotTables("PivotTable1")
                With .PivotFields("All Words")
                    .Orientation = xlRowField
                    .Position = 1
                End With
                .AddDataField ActiveSheet.PivotTables( _
            "PivotTable1").PivotFields("COUNT"), "Sum of COUNT", xlSum
            End With

            .Columns("G:H").Copy
            .Columns("J:K").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Application.CutCopyMode = False
            .Columns("B:B").Copy
            .Columns("M:M").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Application.CutCopyMode = False
            .Range("M1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            ActiveCell.FormulaR1C1 = "ALL WORDS"
            .Range("N1").FormulaR1C1 = "COUNT"
            .Range("N2").FormulaR1C1 = "1"
            .Range(.Range("N1048576"), .Range("N1048576").End(xlUp)).FillDown
            .Columns("M:N").Select
            ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
                                              SourceData:="temp!R1C13:R1048576C14", _
                                              Version:=xlPivotTableVersion15) _
                                      .CreatePivotTable TableDestination:="temp!R1C16:R1C17", _
                                                        TableName:="PivotTable2", _
                                                        DefaultVersion:=xlPivotTableVersion15

            .Cells(1, 16).Select
            With .PivotTables("PivotTable2")
                With .PivotFields("ALL WORDS")
                    .Orientation = xlRowField
                    .Position = 1
                End With
                .AddDataField ActiveSheet.PivotTables( _
            "PivotTable2").PivotFields("COUNT"), "Sum of COUNT", xlSum
            End With

            .Columns("P:Q").Copy
            .Columns("S:T").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
            Application.CutCopyMode = False
            .Range(.Range("S2:T2"), .Range("S2:T2").End(xlDown)).Copy
            .Range("J113487:K113487").Select
            .Paste
            Application.CutCopyMode = False
            .Range("J226972:K226972").ClearContents
            .Range(.Range("J226971:K226971"), .Range("J226971:K226971").End(xlUp)).Select
            ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
                                              SourceData:="temp!R1C10:R226971C11", _
                                              Version:=xlPivotTableVersion15) _
                                      .CreatePivotTable TableDestination:="temp!R1C22:R1C23", _
                                                        TableName:="PivotTable3", _
                                                        DefaultVersion:=xlPivotTableVersion15
            .Cells(1, 22).Select
'            With .PivotTables("PivotTable3")
'                With .PivotFields("Row Labels") '<-- where has this field been defined?
'                .Orientation = xlRowField
'                .Position = 1
'                End With
'                .AddDataField ActiveSheet.PivotTables( _
'                "PivotTable3").PivotFields("Sum of COUNT"), "Sum of Sum of COUNT", xlSum
'            End With
'            .Range("W2").Select
'            .PivotTables("PivotTable3").PivotFields("Row Labels").AutoSort _
'                                                                xlDescending, _
'                                                                "Sum of Sum of COUNT", _
'                                                                ActiveSheet.PivotTables("PivotTable3").PivotColumnAxis.PivotLines(1), _
'                                                                1
'
'            .Range(.Range("V1:W1048573"), .Range("V1:W1048573").End(xlUp)).Copy
        End With
'        Sheets.Add After:=ActiveSheet
'        ActiveSheet.Paste

    End If

End Sub

答案 1 :(得分:0)

我认为你应该用f8逐行调试你的代码...我相信有很多代码可以删除:选择另一个范围后选择范围几乎总是不需要你可以删除previos选项。用一行复制和粘贴...选择是一种不好的做法。检查你的支点...看起来你试图一次又一次地添加相同的总字段。删除滚动线。

不要使用 范围(&#34; E2&#34;)。选择ActiveCell.FormulaR1C1 =&#34; 1&#34;

写 范围(&#34; E2&#34;)。FormulaR1C1 =&#34; 1&#34; 要么 范围(&#34; E2&#34;)。值=&#34; 1&#34;