0

更新了最新的代码。唯一悬而未决的问题是工作簿没有改变,但其他一切都按我的意愿工作:)

在 VBA 中,我试图在不同的工作簿中创建一个具有多个条件的 sumifs 公式,但我在语法上遇到了很大的困难。任何帮助都会很棒:)

WorkbookRecut.Worksheets("Summary").Activate
Dim CountRows As Long
Dim CountRows2 As Long
CountRows = WorkbookRecut.Worksheets("Summary").Range("I" & WorkbookRecut.Worksheets("Summary").Rows.Count - 1).End(xlUp).Row
CountRows2 = CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Range("I" & CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Rows.Count - 1).End(xlUp).Row

CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Activate
Range("O6").Formula = _
"=Sumifs(" & [WorkbookRecut].Sheets("Summary").Range("I9").Address & ":" & [WorkbookRecut].Sheets("Summary").Range("I" & CountRows).Address _
& "," & [WorkbookRecut].Sheets("Summary").Range("A9").Address & ":" & [WorkbookRecut].Sheets("Summary").Range("A" & CountRows).Address _
& "," & [CashBreaksMetricsWorkbookFinal].Worksheets("CSCIG_Cash Breaks Metrics").Range("K6").Address(Rowabsolute:=False) _
& "," & [WorkbookRecut].Sheets("Summary").Range("D9").Address & ":" & [WorkbookRecut].Sheets("Summary").Range("D" & CountRows).Address _
& "," & [CashBreaksMetricsWorkbookFinal].Worksheets("CSCIG_Cash Breaks Metrics").Range("N6").Address(Rowabsolute:=False) & ")"
CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Range("O6:O" & CountRows2).FillDown
4

4 回答 4

0

我们缺少 .Address(External:=True)

感谢大家帮助我到达那里(终于!)

下面的最终代码

Public CashBreaksMetricsWorkbookFinal As Workbook
Public WorkbookRecut As Workbook

Dim CountRows As Long
Dim CountRows2 As Long

CountRows = WorkbookRecut.Worksheets("Summary").Range("I" & WorkbookRecut.Worksheets("Summary").Rows.Count - 1).End(xlUp).Row
CountRows2 = CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Range("I" & CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Rows.Count - 1).End(xlUp).Row

CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Activate
Range("O6").Formula = _
"=Sumifs(" & [WorkbookRecut].Sheets("Summary").Range("I9").Address(External:=True) & ":" & [WorkbookRecut].Sheets("Summary").Range("I" & CountRows).Address(External:=True) _
& "," & [WorkbookRecut].Sheets("Summary").Range("A9").Address(External:=True) & ":" & [WorkbookRecut].Sheets("Summary").Range("A" & CountRows).Address(External:=True) _
& "," & [CashBreaksMetricsWorkbookFinal].Worksheets("CSCIG_Cash Breaks Metrics").Range("K6").Address(Rowabsolute:=False) _
& "," & [WorkbookRecut].Sheets("Summary").Range("D9").Address(External:=True) & ":" & [WorkbookRecut].Sheets("Summary").Range("D" & CountRows).Address(External:=True) _
& "," & [CashBreaksMetricsWorkbookFinal].Worksheets("CSCIG_Cash Breaks Metrics").Range("N6").Address(Rowabsolute:=False) & ")"
CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics").Range("O6:O" & CountRows2).FillDown
于 2021-06-08T00:39:59.450 回答
0

在公式中,您必须双引号现有的引号:

改变

Sheets("Summary")

至:

Sheets(""Summary"")
于 2021-06-08T09:11:01.937 回答
0

创建要添加到单元格的公式字符串时,您需要考虑不同范围相对于要放置公式的工作表的位置。仅调用Address()其中一个输入可能无法满足您的需求。

您可以尝试类似下面的代码将该部分抽象为一个单独的函数:

Sub Tester()
    
    Dim wsSumm As Worksheet, wsCBM As Worksheet
    Dim lr As Long, f
    
    Set wsSumm = WorkbookRecut.Worksheets("Summary")
    Set wsCBM = CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics")
    
    lr = wsSumm.Cells(Rows.Count, "I").End(xlUp).Row

    f = "=SUMIFS(" & RealAddress(wsCBM, wsSumm.Range("I9:I" & lr)) & "," & _
                     RealAddress(wsCBM, wsSumm.Range("A9:A" & lr)) & ",$K6," & _
                     RealAddress(wsCBM, wsSumm.Range("D9:D" & lr)) & ",$N6)"
    
    With wsCBM.Range("O9")
        .Formula = f
    End With
    
End Sub

'get a range address for `rngRef`,
'   suitable for use in a formula on worksheet `ws`
Function RealAddress(ws, rngRef As Range) As String
    Dim s As String
    If ws.Parent Is rngRef.Worksheet.Parent Then 'same workbooks?
        If Not ws Is rngRef.Worksheet Then s = "'" & rngRef.Worksheet.Name & "'!" 'diff. worksheets?
        s = s & rngRef.Address(True, True)
    Else
        s = rngRef.Address(True, True, external:=True) 'different workbooks
    End If
    RealAddress = s
End Function
于 2021-06-07T23:00:15.273 回答
0

对于公式:您可能正在从每个 Ranges 中寻找 .Address 属性。类似于Range1.Address & ":" & Range2.Address获得类似的输出$I$9:$I$307

但是对于您的 Ranges,您需要将CountRowsRange 输入放在里面WorkbookRecut.Sheets("Summary").Range("A" & CountRows),然后将其添加.Address到其中。

我也同意@TimWilliams 的观点,即通过为工作表添加一些昵称,您的公式代码可以极大地提高可读性。

以下是您的代码在纠正了这 3 件事后的样子:

Public CashBreaksMetricsWorkbookFinal As Workbook
Public WorkbookRecut As Workbook

Dim SumSh As Worksheet
Set SumSh = WorkbookRecut.Sheets("Summary")

Dim CountRows As Long
CountRows = SumSh.Range("I" & SumSh.Rows.Count - 1).End(xlUp).Row

Dim CSCIG As Worksheet
Set CSCIG = CashBreaksMetricsWorkbookFinal.Worksheets("CSCIG_Cash Breaks Metrics")

CSCIG.Activate
Range("O9").Formula = _
       "=Sumifs(" & SumSh.Range("I9") & ":" & SumSh.Range("I" & CountRows).Address _
       & "," & SumSh.Range("A9").Address & ":" & SumSh.Range("A" & CountRows).Address _
       & "," & CSCIG.Range("K6").Address _
       & "," & SumSh.Range("D9").Address & ":" & SumSh.Range("D" & CountRows).Address _
       & "," & CSCIG.Range("N6").Address & ")"
       
CSCIG.Range("O9").FillDown
于 2021-06-07T22:14:58.030 回答