简体   繁体   中英

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

I have a spreadsheet where on one page there is a list of names like so:

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

I use this list as my base. I then have a loop that goes through those names and updates a pivot table, the only problem is that I don't know how to add an IF statement to it. For example if Dr. F is not in the pivot table then no information should show, what's happening is, Dr. F is being assigned to the list and then the information for the previous doctor, Dr. E remains. Here is an illustration:

Dr. E is in the Pivot table list

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

Dr. F is not in the Pivot table list but gets the values of Dr. E - no info should be returned.

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

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

Here is my code:

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

Thank you,

If you create a blank record in the pivot table and before vrow +1 you set the pivot table equal to blank. If the physician is not found it will default to blank. I use something similar for my profiles and hcahps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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