从另一个工作簿调用时出现代码错误

时间:2014-06-24 15:06:17

标签: excel excel-vba vba

我已经编写了一些代码来链接我每天使用的3个工作簿并在3之间自动执行一些信息传输。第一个包含的信息通过我编写的一些代码以相关格式复制并粘贴到第二个。在第二张表中,有一些代码,由比现在离开的我更先进的人编写,它重新格式化信息并将其粘贴到第三个工作簿中。独立运行时,第二个工作簿中的宏可以正常工作。但是,当我尝试使用自己的代码(存储在第一个工作簿中)调用它时,它无法正常工作。这是我认为它出错的地方:(这是第二个工作簿中应该创建第三个工作簿的代码)

Public Sub ExportOpenlinkDeals()

...

Set objWbk = ThisWorkbook
Set objWS = objWbk.Worksheets("SwapTrades")

Set objWbkOut = Application.Workbooks.Add
Set objWSOut = objWbkOut.Worksheets(1)

SetOpenLinkHeader objWSOut

如果我在从第二个工作簿调用此宏之前停止我的代码,并按下上述宏的按钮。如果我要求我的代码运行它,它不会创建一个新的工作簿,而是覆盖第二个工作簿中的头文件和第三个工作簿中的头文件(即在工作簿上执行包含调用的宏的SetOpenlinkHeader而不是而不是一个新的,它似乎没有创造)。

我使用Application.Run" Workbookname.xls!子名称"来调用包含上述代码的宏。

如果它是相关的,SetOpenlinkHeader是一个私有子,我用来调用它的代码(如果这有助于沟通,称为Hedge)只是声明为Sub而不是公共或私有。

SetOpenlinkHeader的代码是:

Private Sub SetOpenLinkHeader(objWS As Worksheet)
With objWS
    .Range("A1").Value2 = "Type"
    .Range("B1").Value2 = "Trader id"
    .Range("C1").Value2 = "Trader location"
    .Range("D1").Value2 = "Chain"
    .Range("E1").Value2 = "Buy/sell"
    .Range("F1").Value2 = "Grade"
    .Range("G1").Value2 = "Internal Legal entity"
    .Range("H1").Value2 = "Month"
    .Range("I1").Value2 = "Year"
    .Range("J1").Value2 = "Put/Call"
    .Range("K1").Value2 = "Strike"
    .Range("L1").Value2 = "Quantity Lots Required"
    .Range("M1").Value2 = "Order type"
    .Range("N1").Value2 = "Executing broker"
    .Range("O1").Value2 = "Clearing broker"
    .Range("P1").Value2 = "Quantity Lots Filled"
    .Range("Q1").Value2 = "Price"
    .Range("R1").Value2 = "Electronic market flag"
    .Range("S1").Value2 = "EFP Deal Reference"
    .Range("T1").Value2 = "External legal entity"
    .Range("U1").Value2 = "Cross Entity Flag"
    .Range("V1").Value2 = "Balmo Day"
    .Range("W1").Value2 = "Trad Date"
    .Range("X1").Value2 = "UTI"
End With
End Sub

我尝试将私有子公开,将ThisWorkbook更改为特定的工作簿名称,并更改创建新工作簿的语法顺序,但没有更改结果 - 请帮忙!

出于保密原因,我无法将其复制到任何地方,但会根据要求提供更多信息!

1 个答案:

答案 0 :(得分:0)

好的,基于我试图复制你的问题,我可以告诉你。

我创建了两个文件:workbook1.xlsm和workbook2.xlsm

在workbook1中,我在标准模块中有以下代码,Module1

此代码打开工作簿2并从工作簿2调用ExportOpenLinkDeals过程

Sub Hedge()
Dim wb1 As Workbook
Dim wb2 As Workbook

Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open("c:\users\david_zemens\desktop\workbook2.xlsm")

Application.Run wb2.Name & "!ExportOpenLinkDeals"

End Sub

在workbook2中,我在标准模块中有以下代码,Module1

这是从workbook1调用的ExportOpenLinkDeals过程。此过程还会调用SetOpenLinkHeader,它也在workbook2中。

Sub ExportOpenLinkDeals()
Dim objWbk As Workbook
Dim objWbkOut As Workbook
Dim objWS As Worksheet
Dim objWSOut As Worksheet

    Set objWbk = ThisWorkbook  '## Workbook2
    Set objWS = objWbk.Worksheets(3)

    Set objWbkOut = Application.Workbooks.Add
    Set objWSOut = objWbkOut.Worksheets(1)

    SetOpenLinkHeader objWSOut
End Sub
Private Sub SetOpenLinkHeader(objWS As Worksheet)
With objWS
    MsgBox IIF(.Parent.Name=ThisWorkbook.Name, "error!", "success!")
    .Range("A1").Value2 = "Type"
    .Range("B1").Value2 = "Trader id"
    .Range("C1").Value2 = "Trader location"
    .Range("D1").Value2 = "Chain"
    .Range("E1").Value2 = "Buy/sell"
    .Range("F1").Value2 = "Grade"
    .Range("G1").Value2 = "Internal Legal entity"
    .Range("H1").Value2 = "Month"
    .Range("I1").Value2 = "Year"
    .Range("J1").Value2 = "Put/Call"
    .Range("K1").Value2 = "Strike"
    .Range("L1").Value2 = "Quantity Lots Required"
    .Range("M1").Value2 = "Order type"
    .Range("N1").Value2 = "Executing broker"
    .Range("O1").Value2 = "Clearing broker"
    .Range("P1").Value2 = "Quantity Lots Filled"
    .Range("Q1").Value2 = "Price"
    .Range("R1").Value2 = "Electronic market flag"
    .Range("S1").Value2 = "EFP Deal Reference"
    .Range("T1").Value2 = "External legal entity"
    .Range("U1").Value2 = "Cross Entity Flag"
    .Range("V1").Value2 = "Balmo Day"
    .Range("W1").Value2 = "Trad Date"
    .Range("X1").Value2 = "UTI"
End With
End Sub

此代码按预期工作,并且MsgBox通知我已添加新书,并且标题行数据已写入 新工作簿。

此代码有效。 它与您的实施有何不同?回答这个问题,您会发现问题。