2015-11-27 35 views
2

我有以下.name.value性能PivotItem:从PowerPivot PivotItem解析值的好方法?

[dimCalendar].[MonthName].&[April 2013] 

我只是在那里说:April 2013部分感兴趣。

解析或以其他方式从PivotItem中获取此值的好方法是什么?

+1

在名称上使用“Split”,使用“[”作为分隔符,然后使用“Replace”来除去尾部“]” – Rory

回答

1

如果你只需要最后一个,这将很好地工作:

Sub test_user1283776() 
MsgBox get_PivotItem_From_PowerPivot("[dimCalendar].[MonthName].&[April 2013]") 
End Sub 

Function get_PivotItem_From_PowerPivot(PowerString As String) As String 
    Dim A() As String 
    A = Split(PowerString, ".&[") 
    get_PivotItem_From_PowerPivot = Trim(Replace(A(UBound(A)), "]", "")) 
End Function 
2

解决方法一:分割和替代

Split将是最好的[作为分隔符。获取Split阵列的上限并将尾随]替换为""

解决方法二:正则表达式

添加Microsoft VBregularexpression 5.5作为参考。

Private Function Method(str As String) As String 
Dim regexp As New regexp 
regexp.Pattern = "\[.*\]\[.*\]\[(.*)\]" 
Method = regexp.Replace(str, "$1") 
End Function