2016-04-14 59 views
1

我的宏的目的是簡單地從一張紙上取一些信息並將其傳輸到另一張紙上以防止重新輸入信息。當我通過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 

回答

0

試試吧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操作更有效,並且不涉及剪貼板。

+0

謝謝你,Jeeped。直接值轉移方法比我的原始複製粘貼方法更清潔。我很欣賞快速反饋。 –