我的宏的目的是简单地从一张纸上获取一些信息并将其传输到另一张纸以防止必须重新输入信息。当我通过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 :(得分:0)
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.Selection和ActiveCell属性来确定操作的来源和目标根本不可靠。类似地,直接值传输比Copy / PasteSpecial,Values操作更有效,并且不涉及剪贴板。