需要VBA代码才能在Excel单元格中显示多个切片器选择

时间:2015-08-26 16:40:08

标签: excel vba excel-vba

这是我第一次使用VBA。我需要在excel电子表格中显示切片器选择。每张纸中有15-20个切片器,最多可选50个,因此我不想使用立方体代码在每个单元格中显示一个选择。如果可能的话,我希望他们在一个单元格中用逗号分隔(或者返回"所有"或者#34;无")。

以下是我通过对本网站及其他网站的研究拼凑而成的内容,以及尝试自行编辑(因此可能完全混乱)。

我放弃搜索并寻求帮助!如果你能指出一个简单的"速成课程" VBA基础网站,我也很感激。谢谢。

Public Function GetSlicerItems()
Dim cache As Excel.SlicerCaches
Set cache = ThisWorkbook.SlicerCaches("Slicer_YR_MNTH_CD1")
Dim sItm As Excel.SlicerItem
Dim ICt As Long

For Each sItm In cache.SlicerItems
If sItm.Selected = True Then GetSlicerItems = GetSlicerItems & sItm.Name & ", "
ICt = ICt + 1
If sItm.HasData = False Then
ICt = ICt + 1
End If
Next
If Len(GetSlicerItems) > 0 Then
If ICt = cache.SlicerItems.Count Then
GetSlicerItems = "All Items"
Else
GetSlicerItems = Left(GetSlicerItems, Len(GetSlicerItems) - 2)
End If
Else
GetSlicerItems = "No items selected"
End If
End Function

2 个答案:

答案 0 :(得分:1)

很棒的方法感谢分享。真假如果声明对我不起作用,所以我只是转到下一行,现在它可以正常工作

Public Function GetSlicerItems() As String
    Dim sSlicerItems As String

    Dim cache As Excel.SlicerCache
    Set cache = ThisWorkbook.SlicerCaches("Slicer_AUTH_BHS_GROUP")

    Dim sItm As Excel.SlicerItem
    Dim ICt As Long

    For Each sItm In cache.SlicerItems
        If sItm.Selected = True Then
        sSlicerItems = sSlicerItems & sItm.Name & ", "
        ICt = ICt + 1
        End If
        If sItm.HasData = False Then
            ICt = ICt + 1
        End If
    Next
    If Len(sSlicerItems) > 0 Then
        If ICt = cache.SlicerItems.Count Then
            sSlicerItems = "All Items"
        Else
            sSlicerItems = Left(sSlicerItems, Len(sSlicerItems) - 2)
        End If
    Else
        sSlicerItems = "No items selected"
    End If

    GetSlicerItems = sSlicerItems


End Function

答案 1 :(得分:0)

第一个一般性建议,
使用F8和SUB逐行测试您的代码,并观察窗口以查看变量内容
google for“vba debug”
使用option explicit模式检查变量类型

有了它,你可以轻易找到什么错误。
例如我发现,cache的类型错误 您只需SlicerCache选择一个ThisWorkbook.SlicerCaches ThisWorkbook.SlicerCaches("Slicer_YR_MNTH_CD1")Dim cache As Excel.SlicerCache 'not SlicerCacheS 集合,所以它应该是:

cache

现在您在变量Dim sSlicerItems as String

中拥有目标缓存

为了确保你不会递归调用你的函数,我建议使用临时变量进行任何操作 例如Public Function GetSlicerItems() As String Dim sSlicerItems As String Dim cache As Excel.SlicerCache Set cache = ThisWorkbook.SlicerCaches("Slicer_YR_MNTH_CD1") Dim sItm As Excel.SlicerItem Dim ICt As Long For Each sItm In cache.SlicerItems If sItm.Selected = True Then sSlicerItems = sSlicerItems & sItm.Name & ", " ICt = ICt + 1 If sItm.HasData = False Then ICt = ICt + 1 End If Next If Len(sSlicerItems) > 0 Then If ICt = cache.SlicerItems.Count Then sSlicerItems = "All Items" Else sSlicerItems = Left(sSlicerItems, Len(sSlicerItems) - 2) End If Else sSlicerItems = "No items selected" End If GetSlicerItems = sSlicerItems End Function

我不确定这个if / else语句的正确逻辑,但无论如何你的纠正代码看起来像:

.parent

用于处理所有切片机上的过程,或只有一个切片的切片器, 使用SlicerCache项目的循环切片器示例https://stackoverflow.com/a/20486330/2519073SlicerCache属性(其工作表) 也许您的解决方案需要更多Sub UnMergeFill(Optional ws As Worksheet) If ws Is Nothing Then Set ws = ActiveSheet Dim fndMrg As Range, joinedCells As Range Application.FindFormat.MergeCells = True With ws On Error Resume Next Set fndMrg = .Cells.Find(What:=vbNullString, SearchFormat:=True) Do While Not fndMrg Is Nothing Set joinedCells = fndMrg.MergeArea fndMrg.MergeCells = False 'fndMrg.UnMerge '??? joinedCells.Value = fndMrg.Value Set fndMrg = .Cells.Find(What:=vbNullString, SearchFormat:=True) Loop End With Application.FindFormat.MergeCells = False End Sub 属性 检查https://msdn.microsoft.com/en-us/library/office/ff822652.aspx