簡體   English   中英

基於另一個工作表上的列表的數據透視表循環Excel 2007 VB

[英]Looping Through a Pivot Table based on a list on another sheet Excel 2007 VB

我有一個電子表格,其中一頁上有這樣的名稱列表:

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博士不在“數據透視表”列表中,但獲取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

謝謝,

如果您在數據透視表中以及vrow +1之前創建空白記錄,則將數據透視表設置為空白。 如果找不到醫生,則默認為空白。 我的個人資料和hcahps使用了類似的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM