【问题标题】:Looping Through a Pivot Table based on a list on another sheet Excel 2007 VB基于另一个工作表 Excel 2007 VB 上的列表循环数据透视表
【发布时间】:2014-05-25 23:11:37
【问题描述】:

我有一个电子表格,其中一个页面上有一个名称列表,如下所示:

Name
Dr. A
Dr. B
Dr. C
.
.
.
Dr. (n)

我将此列表用作我的基础。然后我有一个循环遍历这些名称并更新数据透视表,唯一的问题是我不知道如何向其中添加 IF 语句。例如,如果 F 博士不在数据透视表中,则不应显示任何信息,发生的情况是,F 博士被分配到列表中,然后之前的医生 E 博士的信息仍然存在。这是一个插图:

博士。 E 在数据透视表列表中

Name     | Measure 1    | Measure 2 | ...
Dr. E    | 5.5          | 6.0       | ...

博士。 F 不在数据透视表列表中,但获取 Dr. E 的值 - 不应返回任何信息。

Name     | Measure 1    | Measure 2 | ...
Dr. F    | 5.5          | 6.0       | ...

Dr. F    | (blank)      | (blank)   | ... <-- is what it should look like

这是我的代码:

Sub Button1_Click()

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Define variables
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim vPhys As String
Dim vrow As Long
Dim vlastphys As String

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' We want to start on Row 2 of the sheet
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
vrow = 2

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This pushes us to the next row in the PhysListing sheet in order to
' obtain the name of the next physician that we want to generate data
' for
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
nextRow:

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This will select the PhysListing Sheet and make it the active sheet
' it will then select the row number from vrow
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sheets("PhysListing").Activate
Range("A" & CStr(vrow)).Select
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Select the physician by selecting the cell that vrow landed us on
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
vPhys = ActiveCell.Value

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This can be uncommented to see that the above does in fact move us
' down the list of doctors
' MsgBox vPhys <-- uncomment
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This tells us to stop going down the list when the nextRow is empty
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Len(vPhys) < 1 Then
    MsgBox "ALL DONE"
    GoTo subcomplete
End If

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' DO FIELD settings here. This is where we are going to grab data from
' the pivot tables which will update the report data tab so the phys
' report can be generated and saved to disk with the filename being the
' attending physicians name.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sheets("readmit_pivot_trend").Activate
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Attending Physician")
        .CurrentPage = vPhys
    End With

Sheets("alos_pivot_current").Activate
    With ActiveSheet.PivotTables("AlosPivotCurrentTable").PivotFields("Attending Physician")
            .CurrentPage = vPhys
    End With

Sheets("alos_pivot_trend").Activate
    With ActiveSheet.PivotTables("AlosPivotTrendTable").PivotFields("Attending Physician")
            .CurrentPage = vPhys
    End With

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This opens up the report sheet and saves the file to disk
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Sheets("report").Activate

'ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
'    Filename:= _
'    "G:\Phys Report Card\current reports\" & vPhys & ".pdf", _
'    Quality:=xlQualityStandard, _
'    IncludeDocProperties:=True, _
'    IgnorePrintAreas:=False, _
'    OpenAfterPublish:=False

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This forces the vrow to increment by one on the PhysListing sheet
' so that we can get data on the next doctor in the list
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
vrow = vrow + 1
vlastphys = vPhys

GoTo nextRow

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' After we have gone through all the data, this ends the routine that
' the button runs on, we then exit and end the sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
subcomplete:

Exit Sub

End Sub

谢谢,

【问题讨论】:

    标签: excel vba loops if-statement pivot-table


    【解决方案1】:

    如果您在数据透视表中创建空白记录,并且在 vrow +1 之前将数据透视表设置为空白。如果找不到医生,则默认为空白。我对我的个人资料和 hcahps 使用类似的东西。

    【讨论】:

    • 这是一些次优的,虽然我想我可以记录一个宏来查找缺失值等。谢谢你的建议,我会试试看。
    猜你喜欢
    • 1970-01-01
    • 2019-05-23
    • 2017-04-29
    • 2014-03-29
    • 2013-12-18
    • 2018-03-30
    • 1970-01-01
    • 2010-11-28
    • 2018-03-11
    相关资源
    最近更新 更多