基于另一个表Excel Excel VBA上的列表循环通过数据透视表

时间:2014-04-11 17:09:46

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

我有一个电子表格,在一个页面上有一个名称列表如下:

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

谢谢,

1 个答案:

答案 0 :(得分:1)

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