VBA循环透视表Excel 2007
我有一段代码,在一个工作表的列表中循环,然后从相应的透视表中选择一个项目,但这不起作用。数据透视表是由我制作的宏创建的,数据透视表生成得很好。尽管我无法理解为什么当我将透视表的CurrentPage部分设置为我的变量时,它没有设置它。下面是我用来循环的代码:VBA循环透视表Excel 2007,vba,excel,Vba,Excel,我有一段代码,在一个工作表的列表中循环,然后从相应的透视表中选择一个项目,但这不起作用。数据透视表是由我制作的宏创建的,数据透视表生成得很好。尽管我无法理解为什么当我将透视表的CurrentPage部分设置为我的变量时,它没有设置它。下面是我用来循环的代码: Sub m4_HCAHPS_Macro() Dim vPhys2 As String Dim vrow2 As Long Dim vlastphys2 As String vrow2 = 1 nextRow2: Sheets("hc
Sub m4_HCAHPS_Macro()
Dim vPhys2 As String
Dim vrow2 As Long
Dim vlastphys2 As String
vrow2 = 1
nextRow2:
Sheets("hcahps doctors").Activate
Range("A" & CStr(vrow2)).Select
vPhys2 = ActiveCell.Value
If Len(vPhys2) < 1 Then
MsgBox "All Done Here"
GoTo subcomplete
End If
Sheets("hcahps").Activate
With ActiveSheet.PivotTables("HcahpsPivotcurrentTable").PivotFields("Doctor").CurrentPage = vPhys2
End With
With ActiveSheet.PivotTables("HcahpsPivotTrendTable").PivotFields("Doctor").CurrentPage = vPhys2
End With
Sheets("hcahps report").Activate
vrow2 = vrow2 + 1
vlastphys2 = vPhys2
GoTo nextRow2
subcomplete:
Exit Sub
End Sub
子m4\U HCAHPS\U宏()
Dim vPhys2作为字符串
变暗vrow2的长度为
尺寸Vlasthys2 As字符串
vrow2=1
下一步2:
工作表(“hcahps医生”)。激活
范围(“A”和CStr(vrow2))。选择
vPhys2=ActiveCell.Value
如果Len(vPhys2)<1,则
MsgBox“全部完成”
转到子完成
如果结束
工作表(“HCAHP”)。激活
使用ActiveSheet.PivotTables(“HCAHPSPIVOTTCurrentTable”).PivotFields(“Doctor”).CurrentPage=vPhys2
以
使用ActiveSheet.PivotTables(“HcahpsPivotTrendTable”).PivotFields(“医生”).CurrentPage=vPhys2
以
表格(“hcahps报告”)。激活
vrow2=vrow2+1
Vlasthys2=vPhys2
下一站2
次级完成:
出口接头
端接头
如果您有任何建议,我们将不胜感激。以下是一些重写此代码的技巧,以便更容易理解,并且不会使用
活动
和转到
语句的常见陷阱
Sub m4_HCAHPS_Macro()
Dim vPhys2 As String
Dim vrow2 As Long: vrow2 = 1
Dim vlastphys2 As String
Dim wksDoctors As Worksheet
Dim wkshcahps As Worksheet
Set wkshcahps = Sheets("hcahps")
Set wksDoctors = Sheets("hcahps doctors")
vPhys2 = wksDoctors.Range("A" & CStr(vrow2)).Value
Do While (Len(vPhys2) < 1)
wkshcahps.PivotTables("HcahpsPivotcurrentTable").PivotFields("Doctor").CurrentPage = vPhys2
wkshcahps.PivotTables("HcahpsPivotTrendTable").PivotFields("Doctor").CurrentPage = vPhys2
vrow2 = vrow2 + 1
vlastphys2 = vPhys2
vPhys2 = wksDoctors.Range("A" & CStr(vrow2)).Value
Loop
MsgBox "All Done Here"
End Sub
尝试用
语句删除,只使用ActiveSheet.PivotTables(“hcahpspivottcurrenttable”).PivotFields(“Doctor”).CurrentPage=vPhys2
也是1)使用GoTo
不是好的编程风格。2) 好的,我不知道我需要做\uuu
然后.CurrentPage=vPhys2
谢谢你的帮助我真的很感激。谢谢你的链接,我是VBA的新手,所以我很感谢你的建议。如果你想把所有这些都写在一个答案中,那么我可以接受
With ActiveSheet.PivotTables("HcahpsPivotTrendTable").PivotFields("Doctor")
.CurrentPage = vPhys2
End With