正确声明的错误“必需的对象”

时间:2019-06-24 14:27:33

标签: excel vba

从一天开始,我一直在努力地告诉我一个错误“ 424”。

问题是我知道我必须将它放在Dim X As XXX中,即使这样,它也无法正常工作。而且这也不是坏类型的问题。

在这里您可以看到我的确切代码(您无需阅读所有内容):

Sub Macro12()

Dim wsDashboard As Worksheet
Dim wsDeliverables As Worksheet
Dim wsQuarters As Worksheet
Dim wsChallenges As Worksheet
Dim wsMetachallenges As Worksheet
Dim rMetachallenges As Range
Dim rMetachallengesID As Range
Dim rMetachallengesName As Range
Dim rChallenges As Range
Dim rChallengesID As Range
Dim rChallengesName As Range
Dim rQuarters As Range
Dim rQuartersID As Range
Dim rQuartersName As Range
Dim rDeliverables As Range
Dim rTitles As Range
Dim rFoundQuarter As Range
Dim rFoundChallenge As Range
Dim rFoundMetachallenge As Range
Dim rrHierarchicalLevels As Range
Dim rdHierarchicalLevels As Range
Dim rqHierarchicalLevels As Range
Dim rcHierarchicalLevels As Range
Dim rdDebuts As Range
Dim rqDebuts As Range
Dim rcDebuts As Range
Dim rrDebuts As Range
Dim rdFins As Range
Dim rqFins As Range
Dim rcFins As Range
Dim rrFins As Range
Dim rcTypes As Range
Dim rrTypes As Range
Dim rcSegments As Range
Dim rrSegments As Range
Dim rcStrategicalImportances As Range
Dim rrStrategicalImportances As Range
Dim rcKTDLeads As Range
Dim rrKTDLeads As Range
Dim rcOtherKTDLeads As Range
Dim rrOtherKTDLeads As Range
Dim rcTRTLeads As Range
Dim rrTRTLeads As Range
Dim rcOtherTRTLeads As Range
Dim rrOtherTRTLeads As Range
Dim rdStarts As Range
Dim rcStarts As Range
Dim rqStarts As Range
Dim rrStarts As Range
Dim rdEnds As Range
Dim rqEnds As Range
Dim rcEnds As Range
Dim rrEnds As Range
Dim rdUpdatedEnds As Range
Dim rqUpdatedEnds As Range
Dim rcUpdatedEnds As Range
Dim rrUpdatedEnds As Range
Dim rdStatus As Range
Dim rqStatus As Range
Dim rcStatus As Range
Dim rrStatus As Range
Dim rdAdvancements As Range
Dim rqAdvancements As Range
Dim rcAdvancements As Range
Dim rrAdvancements As Range
Dim lTotalDeliverables As Long
Dim lQuarter As Long
Dim lChallenge As Long
Dim lMetachallenge As Long
Dim lNextQuarter As Long
Dim lNextChallenge As Long
Dim lNextMetachallenge As Long
Dim lAddedlines As Long
Dim lTest As Long


Set wsDashboard = ActiveWorkbook.Worksheets("Dashboard") 'variable faisant référence à la spreadsheet "MENU"
Set wsDeliverables = ActiveWorkbook.Worksheets("deliverables") 'variable faisant référence à la spreadsheet "TRT RTI Challenges"
Set wsQuarters = ActiveWorkbook.Worksheets("quarters")
Set wsChallenges = ActiveWorkbook.Worksheets("challenges")
Set wsMetachallenges = ActiveWorkbook.Worksheets("metachallenges")
Set rMetachallenges = Range("t_deliverables").ListObject.ListColumns("id_metachallenge").DataBodyRange
Set rMetachallengesID = Range("t_metachallenges").ListObject.ListColumns("id").DataBodyRange
Set rMetachallengesName = Range("t_metachallenges").ListObject.ListColumns("Metachallenges").DataBodyRange
Set rChallenges = Range("t_deliverables").ListObject.ListColumns("id_challenge").DataBodyRange 'variable faisant référence à la colonne "Associated_challenge" du tableau "t_deliverables" de la spreadsheet "TRT RTI Challenges"
Set rChallengesID = Range("t_challenges").ListObject.ListColumns("id").DataBodyRange
Set rChallengesName = Range("t_challenges").ListObject.ListColumns("Challenges").DataBodyRange
Set rQuarters = Range("t_deliverables").ListObject.ListColumns("id_quarter").DataBodyRange 'variable faisant référence à la colonne "Associated_quarter" du tableau "t_deliverables" de la spreadsheet "TRT RTI Challenges"
Set rQuartersID = Range("t_quarters").ListObject.ListColumns("id").DataBodyRange
Set rQuartersName = Range("t_quarters").ListObject.ListColumns("Quarters").DataBodyRange
Set rDeliverables = Range("t_deliverables").ListObject.ListColumns("RTI Challenge Title").DataBodyRange 'variable faisant référence à la colonne "RTI Challenge Title" du tableau "t_deliverables" de la spreadsheet "TRT RTI Challenges"
Set rTitles = Range("t_dashboard").ListObject.ListColumns("RTI Challenges").DataBodyRange
Set rrHierarchicalLevels = Range("t_dashboard").ListObject.ListColumns("Hierarchical Level").DataBodyRange
Set rdHierarchicalLevels = Range("t_deliverables").ListObject.ListColumns("Hierarchical Level").DataBodyRange
Set rcHierarchicalLevels = Range("t_challenges").ListObject.ListColumns("Hierarchical Level").DataBodyRange
Set rqHierarchicalLevels = Range("t_quarters").ListObject.ListColumns("Hierarchical Level").DataBodyRange
Set rdDebuts = Range("t_deliverables").ListObject.ListColumns("Début").DataBodyRange
Set rqDebuts = Range("t_quarters").ListObject.ListColumns("Début").DataBodyRange
Set rcDebuts = Range("t_challenges").ListObject.ListColumns("Début").DataBodyRange
Set rrDebuts = Range("t_dashboard").ListObject.ListColumns("Début").DataBodyRange
Set rdFins = Range("t_deliverables").ListObject.ListColumns("Fin").DataBodyRange
Set rqFins = Range("t_quarters").ListObject.ListColumns("Fin").DataBodyRange
Set rcFins = Range("t_challenges").ListObject.ListColumns("Fin").DataBodyRange
Set rrFins = Range("t_dashboard").ListObject.ListColumns("Fin").DataBodyRange
Set rrTypes = Range("t_dashboard").ListObject.ListColumns("Type").DataBodyRange
Set rcSegments = Range("t_challenges").ListObject.ListColumns("SEGMENT PCC").DataBodyRange
Set rrSegments = Range("t_dashboard").ListObject.ListColumns("SEGMENT PCC").DataBodyRange
Set rcStrategicalImportances = Range("t_challenges").ListObject.ListColumns("Strategical Importance").DataBodyRange
Set rrStrategicalImportances = Range("t_dashboard").ListObject.ListColumns("Strategical Importance").DataBodyRange
Set rcKTDLeads = Range("t_challenges").ListObject.ListColumns("KTD Lead").DataBodyRange
Set rrKTDLeads = Range("t_dashboard").ListObject.ListColumns("KTD Lead").DataBodyRange
Set rcOtherKTDLeads = Range("t_challenges").ListObject.ListColumns("Other KTD involved").DataBodyRange
Set rrOtherKTDLeads = Range("t_dashboard").ListObject.ListColumns("Other KTD involved").DataBodyRange
Set rcTRTLeads = Range("t_challenges").ListObject.ListColumns("TRT Lead").DataBodyRange
Set rrTRTLeads = Range("t_dashboard").ListObject.ListColumns("TRT Lead").DataBodyRange
Set rcOtherTRTLeads = Range("t_challenges").ListObject.ListColumns("Other TRT / Team involved").DataBodyRange
Set rrOtherTRTLeads = Range("t_dashboard").ListObject.ListColumns("Other TRT / Team involved").DataBodyRange
Set rdStarts = Range("t_deliverables").ListObject.ListColumns("Start (T0)").DataBodyRange
Set rcStarts = Range("t_challenges").ListObject.ListColumns("Start (T0)").DataBodyRange
Set rqStarts = Range("t_quarters").ListObject.ListColumns("Start (T0)").DataBodyRange
Set rrStarts = Range("t_dashboard").ListObject.ListColumns("Start (T0)").DataBodyRange
Set rdEnds = Range("t_deliverables").ListObject.ListColumns("End date").DataBodyRange
Set rcEnds = Range("t_challenges").ListObject.ListColumns("End date").DataBodyRange
Set rqEnds = Range("t_quarters").ListObject.ListColumns("End date").DataBodyRange
Set rrEnds = Range("t_dashboard").ListObject.ListColumns("End date").DataBodyRange
Set rdUpdatedEnds = Range("t_deliverables").ListObject.ListColumns("Updated End date").DataBodyRange
Set rcUpdatedEnds = Range("t_challenges").ListObject.ListColumns("Updated End date").DataBodyRange
Set rqUpdatedEnds = Range("t_quarters").ListObject.ListColumns("Updated End date").DataBodyRange
Set rrUpdatedEnds = Range("t_dashboard").ListObject.ListColumns("Updated End date").DataBodyRange
Set rdStatus = Range("t_deliverables").ListObject.ListColumns("Status").DataBodyRange
Set rqStatus = Range("t_quarters").ListObject.ListColumns("Status").DataBodyRange
Set rcStatus = Range("t_challenges").ListObject.ListColumns("Status").DataBodyRange
Set rrStatus = Range("t_dashboard").ListObject.ListColumns("Status").DataBodyRange
Set rdAdvancements = Range("t_deliverables").ListObject.ListColumns("Deliverable Avancement").DataBodyRange
Set rqAdvancements = Range("t_quarters").ListObject.ListColumns("Deliverable Avancement").DataBodyRange
Set rcAdvancements = Range("t_challenges").ListObject.ListColumns("Deliverable Avancement").DataBodyRange
Set rrAdvancements = Range("t_dashboard").ListObject.ListColumns("Deliverable Avancement").DataBodyRange

'Set rSegment = Range("t_deliverables").ListObject.ListColumns("SEGMENT PCC").DataBodyRange 'variable faisant référence à la colonne "SEGMENT PCC" du tableau "t_deliverables" de la spreadsheet "TRT RTI Challenges"
'Set rStart = Range("t_deliverables").ListObject.ListColumns("Start (T0)").DataBodyRange 'variable faisant référence à la colonne "Start (T0)" du tableau "t_deliverables" de la spreadsheet "TRT RTI Challenges"
'Set rEnd = Range("t_deliverables").ListObject.ListColumns("End date").DataBodyRange 'variable faisant référence à la colonne "End date" du tableau "t_deliverables" de la spreadsheet "TRT RTI Challenges"
'Set rFeedbackDone = Range("t_deliverables").ListObject.ListColumns("Feedback given to TRT Leader ?").DataBodyRange 'variable faisant référence à la colonne "Feedback given to TRT Leader ?" du tableau "t_deliverables" de la spreadsheet "TRT RTI Challenges"
'Set rHierarchicalLevel = Range("t_deliverables").ListObject.ListColumns("Hierarchical Level").DataBodyRange 'variable faisant référence à la colonne "Hierarchical Level" du tableau "t_deliverables" de la spreadsheet "TRT RTI Challenges"
'Set rStatus = Range("t_deliverables").ListObject.ListColumns("Status").DataBodyRange
'Set rNumsChall = Range("t_deliverables").ListObject.ListColumns("N° Chall.").DataBodyRange
'Set rInsertedQuarters = ActiveWorkbook.Worksheets("quarters").Range("R:R")

wsDashboard.Select
Cells.Select
Selection.ClearFormats
ActiveSheet.ListObjects("t_dashboard").TableStyle = ""

Range("t_dashboard").ListObject.DataBodyRange.Delete

lTotalDeliverables = rDeliverables.Cells.Count

'Tri la spreadsheet "deliverables"
ActiveWorkbook.Worksheets("deliverables").ListObjects("t_deliverables").Sort. _
    SortFields.Clear
ActiveWorkbook.Worksheets("deliverables").ListObjects("t_deliverables").Sort. _
    SortFields.Add Key:=Range("t_deliverables[id_metachallenge]"), SortOn:= _
    xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("deliverables").ListObjects("t_deliverables").Sort. _
    SortFields.Add Key:=Range("t_deliverables[id_challenge]"), SortOn:= _
    xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("deliverables").ListObjects("t_deliverables").Sort. _
    SortFields.Add Key:=Range("t_deliverables[id_quarter]"), SortOn:= _
    xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("deliverables").ListObjects("t_deliverables").Sort. _
    SortFields.Add Key:=Range("t_deliverables[id_deliverable]"), SortOn:= _
    xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("deliverables").ListObjects("t_deliverables"). _
    Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

lQuarter = 0
lChallenge = 0
lMetachallenge = 0

lAddedlines = 0

'For i = 2 To lTotalDeliverables + 1
For i = 2 To 10
    lNextQuarter = wsDeliverables.Cells(i, rQuarters.Column).Value
    lNextChallenge = wsDeliverables.Cells(i, rChallenges.Column).Value
    lNextMetachallenge = wsDeliverables.Cells(i, rMetachallenges.Column).Value
    If lNextMetachallenge <> lMetachallenge Then
        wsDashboard.Select
        wsDashboard.Cells(1, 1).Select
        Selection.ListObject.ListRows.Add AlwaysInsert:=True

        Set rFoundMetachallenge = rMetachallengesID.Find(lNextMetachallenge, rMetachallengesID.Cells(rMetachallengesID.Cells.Count), xlValues, xlWhole, , xlNext, False)

        wsMetachallenges.Select
        wsMetachallenges.Cells(rFoundMetachallenge.Row, rMetachallengesName.Column).Select
        Selection.Copy
        wsDashboard.Select
        lTest = rTitles.Column
        'Cells(i + lAddedlines, rTitles.Column).Select

    End If
Next i

在“ lTest = rTitles.Column”行,我得到了错误,而且我真的真的不明白为什么。我唯一了解的是rTitles.Column无法理解。

我写这行是为了理解为什么“'Cells(i + lAddedlines,rTitles.Column).Select”不起作用。确实,在这条线上,我遇到了同样的错误,并且似乎来自rTitles。

感谢任何可以帮助我的人。真的!

亚瑟:)

1 个答案:

答案 0 :(得分:0)

如果rTitles.Address输入错误,则rTitles未设置为范围。 我相信这是由线路引起的

Range("t_dashboard").ListObject.DataBodyRange.Delete

您首先设置范围,但随后将其删除。这就是为什么它以后不再存在的原因。