Excel VBA:将工作簿,工作表对象传递给子例程时出现问题

时间:2019-03-13 19:26:04

标签: excel vba object parameters

我有一个主例程,可以创建一个新的工作簿并与之动态关联(基于单独的输入工作簿/工作表中的数据)。

摘录主要程序:

scipy.optimize

...

Sub MainRoutine()
    Dim NmOutBook As String
    NmOutBook = "Client1Output_" & Format(CStr(Now), "yyyy_mm_dd_hh_mm")

    Dim PosSourceBk, TrnSourceBk, OutputBk As Workbook
    Set PosSourceBk = Workbooks.Open("U:\Documents\Implementations\Client1\Client1Positions.xlsx")
    Set TrnSourceBk = Workbooks.Open("U:\Documents\Implementations\Client1\TradeHistory_0301.xlsx")
    Dim TrnSrcSht, TrnOutSht, PriorTrnOutSht, PosOutSht As Worksheet
    Set TrnSrcSht = TrnSourceBk.ActiveSheet

    'Create workbook to store output sheets
    Set OutputBk = Workbooks.Add

我将我的输出工作簿对象和相应的工作表对象传递给子例程AddXactSheetHeaders,如下所示:

If (SecNm <> PriorSecNm) Then
    Set TrnOutSht = OutputBk.Sheets.Add(after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    TrnOutShtName = CStr(SecNm) + "_b"
    TrnOutSht.Name = TrnOutShtName
    AddXactSheetHeaders OutputBk, TrnOutSht

其结果是更新了单元格A1:M1,但是在错误的工作簿/工作表中。

我尝试通过引用传递对象引用;当我执行此操作时,将更新其他(但仍然错误)的工作簿/工作表。

我肯定想念一些明显的东西,但不知道可能是什么。

任何帮助表示赞赏。谢谢。

2 个答案:

答案 0 :(得分:0)

这是因为在With段中,您没有使用点(。)来指定wb / ws中的更改。

在任何With段中,使用点将“命令”绑定到With段。 Ej:

With ThisWorkbook.Sheets("Sheet5")
    Range("A5").Value = "String Test" 'This changes the Cell A5 of the ActiveSheet
    .Range("A5").Value = "Test 2" 'This Changes the Value of Sheet5, part of the With Segment
End With

只需添加点/句段

Sub AddXactSheetHeaders(ByVal wb, ByVal ws)
    With wb
        With ws
            .Range("A1").Value = "TradeDate"
            .Range("B1").Value = "SettleDate"
            .Range("C1").Value = "Tran ID"
            .Range("D1").Value = "Tranx Type"
            .Range("E1").Value = "Security Type"
            .Range("F1").Value = "Security ID"
            .Range("G1").Value = "SymbolDescription"
            .Range("H1").Value = "Local Amount"
            .Range("I1").Value = "Book Amount"
            .Range("J1").Value = "MOIC Label"
            .Range("K1").Value = "Quantity"
            .Range("L1").Value = "Price"
            .Range("M1").Value = "CurrencyCode"
        End With
    End With
End Sub

答案 1 :(得分:0)

使用 ByRef 通过指针传递工作簿/工作表对象,而不是复制它们,然后将它们用作helper子项中的父引用。要在With ... End With中引用一个Range,请使用前缀句点。

Sub AddXactSheetHeaders(Byref wb as workbook, ByRef ws as worksheet)

  With wb
   With ws
     .Range("A1").Value = "TradeDate"
     .Range("B1").Value = "SettleDate"
     .Range("C1").Value = "Tran ID"
     .Range("D1").Value = "Tranx Type"
     .Range("E1").Value = "Security Type"
     .Range("F1").Value = "Security ID"
     .Range("G1").Value = "SymbolDescription"
     .Range("H1").Value = "Local Amount"
     .Range("I1").Value = "Book Amount"
     .Range("J1").Value = "MOIC Label"
     .Range("K1").Value = "Quantity"
     .Range("L1").Value = "Price"
     .Range("M1").Value = "CurrencyCode"
   End With
  End With 

End Sub

实际上, wb 引用完全没有必要,并且在语法上是错误的。 ws 工作表知道其父工作簿是什么。

Sub AddXactSheetHeaders(ByRef ws as worksheet)

  With ws
     .Range("A1").Value = "TradeDate"
     .Range("B1").Value = "SettleDate"
     .Range("C1").Value = "Tran ID"
     .Range("D1").Value = "Tranx Type"
     .Range("E1").Value = "Security Type"
     .Range("F1").Value = "Security ID"
     .Range("G1").Value = "SymbolDescription"
     .Range("H1").Value = "Local Amount"
     .Range("I1").Value = "Book Amount"
     .Range("J1").Value = "MOIC Label"
     .Range("K1").Value = "Quantity"
     .Range("L1").Value = "Price"
     .Range("M1").Value = "CurrencyCode"
  End With 

End Sub