繁体   English   中英

将变量工作表名称插入其他工作簿的命名范围的Sumif公式

[英]Sumif formula that Inserts variable worksheet name into named range of other workbook

如您在下面的代码中看到的,我在不同的工作簿中匹配工作表名称。 宏找到匹配的工作表名称后,它将执行SUMIF公式。 SUMIF公式内部的命名范围对于每个工作表都是唯一的,但是是一致的。 (即,工作表名称为“ Sheet1” ...命名范围1为“ Sheet1_WEEKENDING”,命名范围2为“ Sheet1_FORECAST”); 这在所有工作表中都是一致的。

我希望SUMIF公式在指定的范围内具有工作表变量。 示例ws = sheet1(命名范围1 =“ ws_WEEKENDING”,命名范围2 =“ ws_FORECAST”)

到目前为止的代码:

Public Sub Baseline()
Dim ws, sh As Worksheet
Dim wbMaster, wbVariance As Workbook
Dim fileOpen As Workbook
Dim folderPath As String
Const VPPName As String = "Master_Vpp.xlsm"
Const VarName As String = "Program Variance Report_Test.xlsm"
'*******************************************************************
'MUST place Master_VPP and Variance Report files in the same folder

Application.ScreenUpdating = False

folderPath = Application.ActiveWorkbook.Path & Application.PathSeparator 'assigning path to get   to both workbooks folder

On Error Resume Next
fileOpen = Workbooks("Master_VPP.xlsm")

If fileOpen Is Nothing Then 'is not open
    Set wbMaster = Application.Workbooks.Open(folderPath & VPPName)
End If

Set wbVariance = ActiveWorkbook    'setting variable quarter variance report

For Each ws In wbVariance.Sheets
Application.ScreenUpdating = False
ws.Activate
    If (ws.Name <> "SUMMARY") And (ws.Name <> "Template") Then
        For Each sh In wbMaster.Sheets
            sh.Activate
            If ws.Name = sh.Name Then
                ws.Range("C20").Activate
                ActiveCell.FormulaR1C1 = _
                    "=SUMIF(Master_VPP.xlsm!HNB_WEEKENDING,RC2,Master_VPP.xlsm!HNB_FORECAST)"
                    '"=SUMIF('[" & wbMaster & "]'!" & sh.Name & "_WEEKENDING,RC2,'[" & wbMaster & "]'!" & sh.Name & "_FORECAST)"

                Selection.AutoFill Destination:=Range("C20:C33")

                'Range("C20").Select
                'ActiveCell.FormulaR1C1 = _
                    "=SUMIF('[" & wbMaster & "]'!" & ws.Name & "_WEEKENDING',RC2,'[" & wbMaster & "]'!" & ws.Name & "_FORECAST)"
                'Selection.AutoFill Destination:=Range("C20:C33")
            Else
            GoTo Cont:
            End If
        Next sh
   Else
     GoTo Cont

续:

   End If
Next ws

End Sub

查看您的代码,看来它永远都行不通-我以为只是需要调整的公式。 也许这样做可以:

Public Sub Baseline()
Dim ws As Worksheet, sh As Worksheet
Dim wbMaster As Workbook, wbVariance As Workbook
Dim fileOpen As Workbook
Dim folderPath As String

Const VPPName As String = "Master_Vpp.xlsm"
Const VarName As String = "Program Variance Report_Test.xlsm"
'*******************************************************************
'MUST place Master_VPP and Variance Report files in the same folder

Application.ScreenUpdating = False

folderPath = Application.ActiveWorkbook.Path & Application.PathSeparator 'assigning path to get   to both workbooks folder

Application.ScreenUpdating = False

Set wbVariance = ActiveWorkbook    'setting variable quarter variance report

On Error Resume Next
Set fileOpen = Workbooks(VPPName)
On Error GoTo 0

If fileOpen Is Nothing Then 'is not open
    Set fileOpen = Application.Workbooks.Open(folderPath & VPPName)
End If

For Each ws In wbVariance.Sheets
    If (ws.Name <> "SUMMARY") And (ws.Name <> "Template") Then
        On Error Resume Next
        Set sh = fileOpen.Sheets(ws.Name)
        On Error GoTo 0
        If Not sh Is Nothing Then
            With ws.Range("C20")
                .FormulaR1C1 = _
                "=SUMIF(" & VPPName & "!" & sh.Name & "_WEEKENDING,RC2," & VPPName & "!" & sh.Name & "_FORECAST)"

                .AutoFill Destination:=ws.Range("C20:C33")
            End With
            Set sh = Nothing
        End If
   End If
Next ws

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM