我正在尝试根据代码设置切片值,以选择连续13个月的范围。如果我在代码中使用该变量,则会创建类型不匹配错误。如果我调试变量并粘贴值而不是变量,代码就可以了。我在尝试使用变量myStr时遗漏了什么?
出错的代码:
Sub Test()
Dim wb As Workbook
Dim myStr As Variant
Dim str2 As String
Set wb = ThisWorkbook
myStr = wb.Worksheets("Filters").Range("G57").Value
str2 = wb.Worksheets("filters").Range("D32").Value
If str2 = "All" Then
wb.SlicerCaches("Slicer_Month1").ClearManualFilter
Else
wb.SlicerCaches("Slicer_Month1").VisibleSlicerItemsList = Array(myStr)
End If
End Sub
有效的代码(代替myStr的值来自?mystr:
Sub Test()
Dim wb As Workbook
Dim myStr As Variant
Dim str2 As String
Set wb = ThisWorkbook
'myStr = wb.Worksheets("Filters").Range("G57").Value
str2 = wb.Worksheets("filters").Range("D32").Value
If str2 = "All" Then
wb.SlicerCaches("Slicer_Month1").ClearManualFilter
Else
wb.SlicerCaches("Slicer_Month1").VisibleSlicerItemsList = Array("[Date].[Month].& _
[2014-02-01T00:00:00]", "[Date].[Month].&[2014-01-01T00:00:00]", "[Date].[Month].& _
[2013-12-01T00:00:00]", "[Date].[Month].&[2013-11-01T00:00:00]", "[Date].[Month].& _
[2013-10-01T00:00:00]", "[Date].[Month].&[2013-09-01T00:00:00]", "[Date].[Month].& _
[2013-08-01T00:00:00]", "[Date].[Month].&[2013-07-01T00:00:00]", "[Date].[Month].& _
[2013-06-01T00:00:00]", "[Date].[Month].&[2013-05-01T00:00:00]", "[Date].[Month].& _
[2013-04-01T00:00:00]", "[Date].[Month].&[2013-03-01T00:00:00]", "[Date].[Month].& _
[2013-02-01T00:00:00]")
End If
End Sub
修改
我的原始代码有一个Join(Transpose(Range),“,”)(见下文)来创建数组而不是在单元格G57中使用合并字符串。我将结果连接到G57,基本上在故障排除工作中创建了所需的字符串。
mystr = _
Join(Application.WorksheetFunction.Transpose(Worksheets("Filters").Range("G43:G55")),",")
.VisibleSlicerItemList = Array(myStr)
我无法发布截图,因为我还没有足够的分数,但是单元格G43:G55包含设置切片器值所需的正确时间/日期标记。格式为“[日期]。[月]。& [YYYY-MM-DD&”T“& HH:MM:SS]”。 G57中的值是G43:G55的连接结果,以逗号分隔并包含引号。
编辑2:
这不是一个真正的解决方案,并且完全不优雅,但时间就是金钱,所以我作为临时解决方案欺骗并宣布13个可能日期值中的每一个的变量。报告包的一部分显示基于选定月份和之前12个月的月度值,因此至少可能的值的数量是固定的。对于需要类似解决方案的其他人来说,它并没有真正解决问题 - 想象100多个可能的选择,畏缩!
Private Sub Worksheet_Calculate()
Dim wb As Workbook
Dim str2 As String
Dim mstr1 As Variant
Dim mstr2 As Variant
Dim mstr3 As Variant
Dim mstr4 As Variant
Dim mstr5 As Variant
Dim mstr6 As Variant
Dim mstr7 As Variant
Dim mstr8 As Variant
Dim mstr9 As Variant
Dim mstr10 As Variant
Dim mstr11 As Variant
Dim mstr12 As Variant
Dim mstr13 As Variant
'Other code runs here
Set wb = ThisWorkbook
mstr1 = wb.Worksheets("Filters").Range("G43").Value
mstr2 = wb.Worksheets("Filters").Range("G44").Value
mstr3 = wb.Worksheets("Filters").Range("G45").Value
mstr4 = wb.Worksheets("Filters").Range("G46").Value
mstr5 = wb.Worksheets("Filters").Range("G47").Value
mstr6 = wb.Worksheets("Filters").Range("G48").Value
mstr7 = wb.Worksheets("Filters").Range("G49").Value
mstr8 = wb.Worksheets("Filters").Range("G50").Value
mstr9 = wb.Worksheets("Filters").Range("G51").Value
mstr10 = wb.Worksheets("Filters").Range("G52").Value
mstr11 = wb.Worksheets("Filters").Range("G53").Value
mstr12 = wb.Worksheets("Filters").Range("G54").Value
mstr13 = wb.Worksheets("Filters").Range("G55").Value
str2 = wb.Worksheets("filters").Range("D32").Value
If str2 = "All" Then
wb.SlicerCaches("Slicer_Month1").ClearManualFilter
Else
wb.SlicerCaches("Slicer_Month1").VisibleSlicerItemsList = Array(mstr1, mstr2, mstr3, mstr4, mstr5, mstr6, mstr7, mstr8, mstr9, mstr10, mstr11, mstr12, mstr13)
End If
'More code runs here
ErrHandler:
RestoreXL
Exit Sub
End Sub
答案 0 :(得分:1)
尝试此操作(基于G43:G55中的值)
Sub Test()
Dim wb As Workbook
Dim shtF as Worksheet
Dim myArray
Dim str2 As String
Set wb = ThisWorkbook
Set shtF = wb.Worksheets("Filters")
str2 = shtF.Range("D32").Value
myArray = Get1DArray( shtF.Range("G43:G55") )
With wb.SlicerCaches("Slicer_Month1")
If str2 = "All" Then
.ClearManualFilter
Else
.VisibleSlicerItemsList = myArray
End If
End With
End Sub
'get a 1-D zero-based array of values from a single-row/column
' range of cells
Function Get1DArray(rng As Range)
Dim arr
arr = Application.Transpose(rng.Value)
If rng.Rows.Count = 1 Then arr = Application.Transpose(arr)
'convert from 1-based to zero-based array
Get1DArray = Split(Join(arr, vbNull), vbNull)
End Function
答案 1 :(得分:0)
<强> EDIT1:强>
如果确实你的所有Filters
都在G57中,正如Tim指出的那样,你也可以尝试下面的内容:)
Sub AnotherPossibleWay()
Dim str1 As String, str2 As String
Dim myStr As Variant
Dim wb As Workbook
Set wb = Thisworkbook
str1 = Replace(wb.Worksheets("Filters").Range("G57").Value, ",", """,""")
str1 = """" & str1 & """"
myStr = Evaluate("{" & str1 & "}")
str2 = wb.Worksheets("Filters").Range("D32").Value
If str2 = "All" Then
wb.SlicerCaches("Slicer_Month1").ClearManualFilter
Else
wb.SlicerCaches("Slicer_Month1").VisibleSlicerItemsList = myStr
End If
End Sub
希望这不会让你感到困惑。
最重要的是,将您的Range Value
正确转换为Array
。
答案 2 :(得分:0)
编辑如下。蒂姆是对的。试试这个:
Sub Test()
Dim wb As Workbook
Dim myStr As Variant
Dim str2 As String
Set wb = ThisWorkbook
myStr = Split(wb.Worksheets("Filters").Range("G57").Value, ",")
str2 = wb.Worksheets("filters").Range("D32").Value
If str2 = "All" Then
wb.SlicerCaches("Slicer_Month1").ClearManualFilter
Else
wb.SlicerCaches("Slicer_Month1").VisibleSlicerItemsList = myStr
End If
End Sub