调用子运行时错误“ 424”时发生错误:必需对象

时间:2020-06-25 07:06:26

标签: excel vba

大图片

  1. 浏览列表并为列表中的每个项目创建一个标签(工作中)
  2. 在列表中创建一个超链接,以链接到关联的工作表(工作中)
  3. 在每个工作表上创建基本标题信息,并超链接回到索引表(工作中)
  4. 为索引表中相应单元格中列出的每个引用插入一个按钮,并为该pdf,doc或docx文件提供超链接(无效,正在进行中)

当前问题

调用要插入按钮的子控件时,出现“需要对象”错误(请参见最后的图片)。

代码的主要部分如下:

Sub CreateTabs()

Dim ws As Worksheet
Dim NameArray As Variant
Dim LastRow As Long
Dim x As Long
Dim y As Long
Dim z As Long
Dim ReferenceCount As Long
Dim RefSplit() As Variant

    LastRow = FindLastRow
    Set ws = ThisWorkbook.Sheets(1)
    NameArray = ws.Range(ws.Cells(2, 1), ws.Cells(LastRow, 1)).Value
    For x = LBound(NameArray) To UBound(NameArray)
        ThisWorkbook.Sheets.Add(after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name = NameArray(x, 1)
        'ws.Hyperlinks.Add ws.Cells(x + 1, 1), "", ThisWorkbook.Sheets(NameArray(x, 1)).Cells(1, 1).Address(External:=True), NameArray(x, 1), NameArray(x, 1)
        With ThisWorkbook.Sheets(NameArray(x, 1))
            ws.Hyperlinks.Add ws.Cells(x + 1, 1), "", .Cells(1, 1).Address(External:=True), .Name, .Name
            .Hyperlinks.Add .Cells(1, 1), "", ws.Cells(1, 1).Address(External:=True), "Item List", "ITEM LIST"
            .Cells(2, 1) = "Item"
            .Cells(3, 1) = "Description"
            .Cells(4, 1) = "U.O.M."
            .Cells(6, 1) = "Specifications"
            .Cells(2, 2).Formula = "=RIGHT(CELL(""filename"",$B$2),LEN(CELL(""filename"",$B$2))-FIND(""]"",CELL(""filename"",$B$2)))"
            .Cells(3, 2).Formula = "=VLOOKUP($B$2,Sheet1!$A$2:$D$" & LastRow & ",2,0)"
            .Cells(4, 2).Formula = "=VLOOKUP($B$2,Sheet1!$A$2:$D$" & LastRow & ",4,0)"
            
            ReferenceCount = Num_Characters_In_String(ws.Cells(x + 1, 3).Value, ", ") + 1
            ReDim RefSplit(1 To ReferenceCount,1)
            If ReferenceCount > 1 Then
                RefSplit = ReferenceSplit(ws.Cells(x + 1, 3).Value)
            Else
                RefSplit(1,1) = ws.Cells(x + 1, 3).Value
            End If
            
            z = 1
            
            For y = 1 To ReferenceCount
                If y > z * 5 Then z = z + 1
'*************************************************************
                Call Insertbutton(z, y - (z - 1) * 5, RefSplit(y, 1).Value, ThisWorkbook.Sheets(NameArray(x, 1)))
'*************************************************************
            Next y
                
        End With
    Next x
End Sub

现在,被调用的子项如下所示:

Sub Insertbutton(btnrow As Long, btncol As Long, btnName As String, ws As Worksheet)

Dim btn As Button
Dim rng As Range

    Application.ScreenUpdating = False
    ws.Buttons.Delete 'probably do not need as it is fresh sheet

    Set rng = ws.Cells(btnrow + 6, btncol + 1)
    Set btn = ws.Buttons.Add(rng.Left, rng.Top, rng.Width, rng.Height)
    
    With btn
        If Left(btnName, 1) = "F" Then
            If Num_Characters_In_String(btnName, "-") = 2 Then
                .OnAction = "P:\2019\1234-name space\08. Working\Specifications\Section F" & btnName & "*.doc*"
            Else
                .OnAction = "P:\2019\1234-name space\10. Construction\01. Tender\F\" & btnName & ".pdf"
            End If
        Else
            .OnAction = "P:\2019\1234-name space\10. Construction\01. Tender\OPSS\OPSS*" & btnName & "*.pdf"
        End If
        .Caption = btnName
        .Name = btnName
    End With
    Application.ScreenUpdating = True

End Sub

Error Pic

问题

缺少的对象是什么?我的通话有什么问题?

(我预见到与文件链接有关的一些问题,但是我在调​​试中还没有到此地步,那将是一个不同的问题。可以这么说来尽量避免使水混乱)

我确实读过this question,所以我相信通话()的格式正确,但是我可能错了

1 个答案:

答案 0 :(得分:0)

RefSplit(y,1).Value 导致错误。 RefSplit(y,1)是正确的。

请勿对数组使用.value。因为它用于范围对象,所以会发生对象错误。

Call Insertbutton(z, y - (z - 1) * 5, RefSplit(y, 1).Value, ThisWorkbook.Sheets(NameArray(x, 1)))

但是,还有另一个错误,并且参数的类型无法匹配。应该使用字符串变量。

Dim myString As String
myString = RefSplit(y, 1)
Call Insertbutton(Z, y - (Z - 1) * 5, myString, ThisWorkbook.Sheets(NameArray(x, 1)))