Page 1 of 1

อยากทราบการอ้างถึงตัวแปรแทน field ในpivotable

Posted: Mon Jun 22, 2015 8:13 pm
by akekorn
สวัสดีครับเพื่อนสมาชิกทุกท่าน
ผมมีปัญหาอยากรบกวนเพื่อนๆช่วยแก้ให้ครับคือผมได้เขียนมาโครขึ้นมาแต่เมื่อมองแล้วมันสามารถยุบรวมโดยอาศัย loop ได้แต่ผมไม่ทราบว่าควรจะแกไข้การอ้าง field ในตารางได้อย่างไรเนื่องชื่อfield ไม่เหมือนจึงอยากรบกวนขอความรู้เพื่อนๆโดยผมได้แนบ โค้ดที่ทำไว้มาด้วยครับ

Code: Select all

Sub akemth()
Dim a, aa As Integer
Dim b As String
Dim i As Integer
Dim ii As Integer

Application.ScreenUpdating = False
    ActiveWorkbook.RefreshAll
    
aa = Month(Date)
a = aa
b = Format(a, "00")
For ii = 1 To 4
    Sheets(ii).Select
        'Sheet1.Select
    If ii = 1 Then
             With ActiveSheet.PivotTables("PivotTable1").PivotFields("Jusify_Month ")
                  .PivotItems(b).Visible = True
                  For i = a To 12
                                     a = a - 1
                                     If a >= 1 And a <= 12 Then
                                             b = Format(a, "00")
                                            .PivotItems(b).Visible = False
                                    End If
                  Next i
                  a = aa
              End With
        ElseIf ii = 2 Then
            With ActiveSheet.PivotTables("PivotTable2").PivotFields("OnScrMonth2")
                 .PivotItems(b).Visible = True
                  For i = a To 12
                                     a = a - 1
                                     If a >= 1 And a <= 12 Then
                                             b = Format(a, "00")
                                            .PivotItems(b).Visible = False
                                    End If
                  Next i
                  a = aa
             End With
        ElseIf ii = 3 Then
             With ActiveSheet.PivotTables("PivotTable3").PivotFields("Jusify_Month2")
             .PivotItems(b).Visible = True
                  For i = a To 12
                                     a = a - 1
                                     If a >= 1 And a <= 12 Then
                                             b = Format(a, "00")
                                            .PivotItems(b).Visible = False
                                    End If
                  Next i
                  a = aa
            End With
        ElseIf ii = 4 Then
             With ActiveSheet.PivotTables("PivotTable4").PivotFields("OnScrMonth2")
             .PivotItems(b).Visible = True
                  For i = a To 12
                                     a = a - 1
                                     If a >= 1 And a <= 12 Then
                                             b = Format(a, "00")
                                            .PivotItems(b).Visible = False
                                    End If
                  Next i
                  a = aa
            End With
        End If
    Next ii
     
ActiveWorkbook.Save
End Sub
ซึ่งผมมองว่าควรยุบให้เป็นแบบนี้ครับแต่ไม่ทราบว่าควรจะใส่ตัวแปรแทน field ในpivot ได้อย่างไร

Code: Select all

Sub test()
Dim a, aa As Integer
Dim b As String
Dim i As Integer
Dim ii As Integer

Application.ScreenUpdating = False
    ActiveWorkbook.RefreshAll
    
aa = Month(Date)
a = aa
b = Format(a, "00")
For ii = 1 To 4
    Sheets(ii).Select
        
    If ii = 1 Then
             With ActiveSheet.PivotTables("PivotTable"&ii).PivotFields(i)
                  .PivotItems(b).Visible = True
                  For i = a To 12
                                     a = a - 1
                                     If a >= 1 And a <= 12 Then
                                             b = Format(a, "00")
                                            .PivotItems(b).Visible = False
                                    End If
                  Next i
                  a = aa
              End With
End Sub
รบกวนด้วยครับ
ขอบคุณครับ

Re: อยากทราบการอ้างถึงตัวแปรแทน field ในpivotable

Posted: Tue Jun 23, 2015 5:40 pm
by snasui
:D แนบไฟล์มาด้วยจะได้ช่วยทดสอบได้ครับ