dynamic表格值filter中的date转换错误

使用excel-Access VBA环境。 excel页面中的vba代码将date单元格的值分配给datefilter。 由于我总是以巴拿马的语言环境工作dd / mm / yyyy格式,因此代码在12日以后的date(即指定月份没有歧义)的情况下工作正常,但是对于dateless于13的date,它将date转换为数字值,我收到一条错误消息,说42502(例如可能12,2016年)不是filter的有效值。 当一天过去12日,它工作正常。 我如何捕获这个错误并解决它?

代码:

ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh ' Range R1 contains the value we desire to filter the date of dynamic table ' Range B1 contains the date filter of the dynamic table Range("B1").Select ActiveSheet.PivotTables("PivotTable1").PivotFields("fecha").ClearAllFilters Application.ScreenUpdating = False 'ActiveSheet.Range("B1") = Range("R1").Value ff = Range("R1").Value ActiveSheet.Range("B1") = Right("00" & Day(ff), 2) & "/" & Right("00" & Month(ff), 2) & "/" & Year(ff) Application.ScreenUpdating = True 

这是我试图解决这个问题的很多方法之一,但是只能在本月12日之后的几天内工作

如果你发布了一些代码,这将是有帮助的,但是我相信问题是你需要在代码中指定你使用的是什么格式,而不是让VBA为你猜。 由于微软是一家美国公司,我们使用MM / DD / YYYY格式,可能是美国格式的默认格式,但是到了13号时,默认情况下是非美国格式。

尝试这个:

 Dim lastRow, i As Long Dim datStg As String lastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row For i = 2 To lastRow dateStg = Cells(i, "R").Text If datStg <> "" Then Cells(i, "B").Value = DateSerial(Mid(dateStg, 8, 4), Mid(dateStg, 5, 2), Mid(dateStg, 2, 2)) Cells(i, "B").NumberFormat = "dd/mm/yyyy" End If Next i