gpt4 book ai didi

vba - 使用 VBA Excel 选择开始日期 - 结束日期

转载 作者:行者123 更新时间:2023-12-04 20:33:51 24 4
gpt4 key购买 nike

我的代码有些问题。我想用 VBA 按钮选择开始日期和结束日期。

这是我的代码。有人可以帮助我吗?多谢...

(对不起,我的英语不好)。

我的代码:

Sub CARI()
Dim objname As String
Dim jumpv As Integer
Dim I As Integer
Dim S1 As Date
Dim S2 As Date

Application.DisplayAlerts = False
Application.ScreenUpdating = False

Sheets("Dashboard").Select
objname = Cells(5, 5).Value
S1 = Cells(6, 4).Value
S2 = Cells(6, 9).Value
jumpv = 4

Worksheets("PV").Activate
For I = 4 To jumpv
Application.StatusBar = "Loading.. ( " & Round(I / jumpv * 100, 0) & ")%"

Sheets("PV").Select
ActiveSheet.PivotTables("PV" & I).PivotFields("REGION").ClearAllFilters
ActiveSheet.PivotTables("PV" & I).PivotFields("REGION").CurrentPage = objname
ActiveSheet.PivotTables("PV" & I).PivotFields("DAY").ClearAllFilters
ActiveSheet.PivotTables("PV" & I).PivotFields("DAY").PivotFilters.Add _
Type:=xlDateBetween, Value1:=S1, Value2:=S2
ActiveSheet.PivotTables("PV" & I).PivotFields("DAY").AutoSort _
xlAscending, "DAY"
Next I
Sheets("Dashboard").Select
Application.StatusBar = ""
MsgBox "Done!"
End Sub

capture PV table

最佳答案

试试下面的代码,代码内的解释为注释:

Option Explicit

Sub CARI()

Dim objname As String
Dim jumpv As Integer
Dim I As Integer
Dim S1 As Date
Dim S2 As Date

Application.DisplayAlerts = False
Application.ScreenUpdating = False

With Sheets("Dashboard") ' <-- use With instead of Activate or Select the sheet
objname = .Cells(5, 5).Value
S1 = .Cells(6, 4).Value
S2 = .Cells(6, 9).Value
End With

jumpv = 4

With Worksheets("PV")
For I = 4 To jumpv
Application.StatusBar = "Loading.. ( " & Round(I / jumpv * 100, 0) & ")%"

.PivotTables("PV" & I).PivotFields("REGION").ClearAllFilters
.PivotTables("PV" & I).PivotFields("REGION").CurrentPage = objname
.PivotTables("PV" & I).PivotFields("DAY").ClearAllFilters

' when filtering dates, safest way is to covert to Double (their actual value, not their format)
.PivotTables("PV" & I).PivotFields("DAY").PivotFilters.Add _
Type:=xlDateBetween, Value1:=CDbl(S1), Value2:=CDbl(S2)
.PivotTables("PV" & I).PivotFields("DAY").AutoSort xlAscending, "DAY"
Next I
End With

Sheets("Dashboard").Select

Application.StatusBar = ""
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "Done!"

End Sub

关于vba - 使用 VBA Excel 选择开始日期 - 结束日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45411352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com