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

时间:2014-06-26 14:23:28

标签: excel vba excel-vba

正如您在下面的代码中看到的,我在不同的工作簿中匹配工作表名称。一旦宏找到匹配的工作表名称,它就会执行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

1 个答案:

答案 0 :(得分:0)

检查你的代码,它似乎从未奏效 - 我原以为它只是需要调整的公式。也许这样做会:

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