首页 文章

VBA:从不同的工作表调用时运行时错误1004?

提问于
浏览
0

如果通过工作表“sheetCTF”调用此子程序,则它可以正常工作 . 如果通过工作表“sheetEXTRACTED”调用它,那么我得到

“运行时错误1004 - 对象'_Worksheet'的方法'范围'失败”

为什么?

Public Const ctfHeadingRow As Long = 1
Public Const ctfLastRow As Long = 200

Sub SUB_copyCtfColsToExtracted(sourceCol As Long, destCol As Long)

sheetCTF.Range(Cells(ctfHeadingRow, sourceCol), Cells(ctfLastRow,sourceCol)).SpecialCells(xlCellTypeVisible).Copy _
Destination:=sheetEXTRACTED.Range(Cells(extractedFirstRow, destCol).Address)

End Sub

(工作表设置在不同的模块中) .

1 回答

  • 1

    我建议:

    Sub SUB_copyCtfColsToExtracted(sourceCol As Long, destCol As Long)
    
    sheetCTF.Range(sheetCTF.Cells(ctfHeadingRow, sourceCol), sheetCTF.Cells(ctfLastRow,sourceCol)).SpecialCells(xlCellTypeVisible).Copy _
    Destination:=sheetEXTRACTED.Range(sheetEXTRACTED.Cells(extractedFirstRow, destCol).Address)
    
    End Sub
    

    这应该可以解决问题 . 如果没有明确定义对诸如“range()”或“Cells”之类的对象的引用,则会出现错误 .

相关问题