将值粘贴到另一个工作簿的单元格中时,VBA运行时错误1004“应用程序定义的错误或对象定义的错误”

时间:2019-04-30 17:15:56

标签: excel vba

我不知道为什么会出现此错误。我无法找到任何人得到类似的代码,得到此错误。

主要目标是从起始工作簿运行此代码,跳转到目标工作簿以获取3个单元格地址,然后跳转回起始工作簿,并在目标工作簿中粘贴引用这些单元格的公式。

我认为正确制定公式将是困难的部分。但是一切正常,除了不允许我将最终的公式字符串粘贴到单元格中。给我运行时错误1004“应用程序定义的错误或对象定义的错误”。我在这里想念什么?

Sub DefeatRunTimeError()

'Starting Workbook
Dim startWB As Workbook
Dim GENTemplate As Worksheet
'Destination Workbook
Dim destWB As Workbook
Dim destBackgroundFormulas As Worksheet
'Other Variables
Dim TabName As String
Dim bLoc As Range
Dim bTSD As Range
Dim bTGL As Range
Dim bRU As Range
'Set Starting Workbook
TabName = ActiveSheet.Name
Set startWB = ActiveWorkbook
Set GENTemplate = startWB.Sheets(TabName)

'Define Filename of Destination Workbook
MasterLoc = startWB.Names("MasterProjectTrackerLocation").RefersToRange.Value

    'Open Destination Workbook
            Workbooks.Open (MasterLoc)

    'Set Destination Workbook
        Set destWB = ActiveWorkbook
        Set destBackgroundFormulas = destWB.Worksheets("Background Formulas")

    'Define ranges
        destBackgroundFormulas.Activate
        Set bLoc = destBackgroundFormulas.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        bLoc.Value = TabName
        Set bTSD = bLoc.Offset(0, 1)
        Set bTGL = bLoc.Offset(0, 2)
        Set bRU = bLoc.Offset(0, 3)

    'Write Formula referencing these cells
        strfile = Replace(ThisWorkbook.FullName, ThisWorkbook.Name, "[" & ThisWorkbook.Name & "]")
        thefilename = "='" & strfile & "Background Formulas" & "'!"
        TargetStartDate = thefilename & bTSD.Address
        TargetGoLive = thefilename & bTGL.Address
        ReviewUpdate = thefilename & bRU.Address

    'Paste Formulas into Original Workbook
        Set bTSD = GENTemplate.Range("b12")
        Set bTGL = GENTemplate.Range("b13")
        Set bRU = GENTemplate.Range("a4")
    'ERROR GIVEN ON THE LINE BELOW RUNTIME 1004
        bTSD.Value = TargetStartDate
        bTGL.Value = TargetGoLive
        bRU.Value = ReviewUpdate

 End Sub

编辑版本

Sub DefeatRunTimeError()

'Starting Workbook
Dim startWB As Workbook
Dim GENTemplate As Worksheet
'Destination Workbook
Dim destWB As Workbook
Dim destBackgroundFormulas As Worksheet
'Other Variables
Dim TabName As String
Dim bLoc As Range
Dim bTSD As Range
Dim bTGL As Range
Dim bRU As Range
'Set Starting Workbook
TabName = ActiveSheet.Name
Set startWB = ActiveWorkbook
Set GENTemplate = startWB.Sheets(TabName)

'Define Filename of Destination Workbook
MasterLoc = startWB.Names("MasterProjectTrackerLocation").RefersToRange.Value

    'Open Destination Workbook
        Workbooks.Open (MasterLoc)

    'Set Destination Workbook
        Set destWB = Application.Workbooks.Open(MasterLoc)
        Set destBackgroundFormulas = destWB.Worksheets("Background Formulas")


    'Define ranges
        destBackgroundFormulas.Activate
        strfile = Replace(destWB.FullName, destWB.Name, "[" & destWB.Name & "]")
        thefilename = "='" & strfile & "Background Formulas" & "'!"

        Set bLoc = destBackgroundFormulas.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        bLoc.Value = TabName
        Set bTSD = bLoc.Offset(0, 1)
        Set bTGL = bLoc.Offset(0, 2)
        Set bRU = bLoc.Offset(0, 3)

    'Write Formula referencing these cells

        TargetStartDate = thefilename & bTSD.Address
        TargetGoLive = thefilename & bTGL.Address
        ReviewUpdate = thefilename & bRU.Address

    'Paste Formulas into Original Workbook
        Set bTSD = GENTemplate.Range("b12")
        Set bTGL = GENTemplate.Range("b13")
        Set bRU = GENTemplate.Range("a4")

        'MsgBox (TargetStartDate)

        bTSD.Formula = TargetStartDate
        bTGL.Formula = TargetGoLive
        bRU.Formula = ReviewUpdate

 End Sub

返回值:TargetStartDate:“ ='Z:\ Master Projects [Master Projects.xlsm]背景公式'!$ B $ 16”:变量/字符串

对于TargetStartDate,它会产生正确的公式。

0 个答案:

没有答案