【问题标题】:Code works when running via editor but not when clicking hyperlink代码在通过编辑器运行时有效,但在单击超链接时无效
【发布时间】:2016-08-06 11:24:06
【问题描述】:

我的宏的目的是简单地从一张表中获取一些信息并将其转移到另一张表中,以防止重新输入信息。当我通过 VBA 编辑器运行代码时,该代码运行良好,但在我尝试通过超链接运行它时导致运行时错误“1004”:应用程序定义或对象定义错误。我知道超链接链接到正确的宏。怎么回事?

Sub Insert_PCO_Row()

    ' Insert_PCO_Row Macro
    ' Inserts PCO information into COR log if COR number is entered in COR number column in "Sub Pricing" Worksheet.

    Dim corNum As Range
    Dim nextOpen As Range

    Sheets("Sub Pricing").Select
    Range("C3").Select

    Set corNum = Sheet6.Range("A1:A1000")

    Do Until Selection.Offset(0, -1) = ""
    'Checks if COR # is entered in "Sub Pricing" tab OR if the COR # is already entered in "COR Log" tab.
    If Selection.Value = "" Or Application.WorksheetFunction.CountIf(corNum, Selection.Value) > 0 = True Then
        Selection.Offset(1, 0).Select
    Else
        Set nextOpen = Sheet6.Range("A9").End(xlDown).Offset(1, 0)
        Selection.Copy
            nextOpen.PasteSpecial xlPasteValues
        Selection.Offset(0, 1).Copy
            nextOpen.Offset(0, 1).PasteSpecial xlPasteValues
        Selection.Offset(0, -2).Copy
            nextOpen.Offset(0, 2).PasteSpecial xlPasteValues
        Selection.Offset(0, -1).Copy
            nextOpen.Offset(0, 3).PasteSpecial xlPasteValues
        Selection.Offset(0, 7).Copy
            nextOpen.Offset(0, 7).PasteSpecial xlPasteValues
        Selection.Offset(1, 0).Select
    End If

    Loop

    Sheets("COR Log").Select

End Sub

【问题讨论】:

    标签: vba excel hyperlink macros


    【解决方案1】:

    试试without using .Select

    Option Explicit
    
    Sub Insert_PCO_Row()
        ' Insert_PCO_Row Macro
        ' Inserts PCO information into COR log if COR number is entered in COR number column in "Sub Pricing" Worksheet.
    
        Dim rw As Long, nrw As Long
    
        With Worksheets("Sub Pricing")
            For rw = 3 To .Cells(Rows.Count, 2).End(xlUp).Row
                With .Cells(rw, 3)
                    If CBool(Len(.Value2)) And _
                       Not IsError(Application.Match(.Value2, sheet6.Columns(1), 0)) Then
                        nrw = sheet6.Cells(Rows.Count, "A").End(xlUp).Row + 1
                            sheet6.Cells(nrw, 1) = .Value
                            sheet6.Cells(nrw, 2) = .Offset(0, 1).Value
                            sheet6.Cells(nrw, 3) = .Offset(0, -2).Value
                            sheet6.Cells(nrw, 4) = .Offset(0, -1).Value
                            sheet6.Cells(nrw, 8) = .Offset(0, 7).Value
                    End If
                End With
            Next rw
        End With
    
        Worksheets("COR Log").Select
    
    End Sub
    

    使用Range .Select 方法并依靠Application.SelectionActiveCell 属性来识别操作的源和目标根本不可靠。同样,直接值传输比 Copy/PasteSpecial、Values 操作更有效,并且不涉及剪贴板。

    【讨论】:

    • 谢谢你,吉普德。直接价值转移方法比我原来的复制粘贴方法干净得多。感谢您的快速反馈。
    猜你喜欢
    • 2018-05-29
    • 2019-07-19
    • 2015-10-09
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多