我正在使用一個函數,我在互聯網上找到提取切片器(即連接到表,而不是數據透視表)並將其存儲在單元格中的值。該功能包括增加application.volatile
,這很好地工作,也在Refresh Excel VBA Function Results這個問題中討論過。刷新用戶定義的功能,無application.volatile
問題是,由於volatile函數,我的OpenSolver模型無法運行。因爲每次迭代Excel都在進行一次計算,這使得OpenSolver認爲Excel沒有爲建模做好準備。
是否有方法在每次切片器更改值時更新單元格的值,而不使用application.volatile
?
我使用已經嘗試過:
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveWorkbook.Sheets("Dashboard").Range("B7").Formula = _
ActiveWorkbook.Sheets("Dashboard").Range("B7").Formula
End Sub
和
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveWorkbook.Sheets("Dashboard").Range("B7").Calculate
End Sub
,我使用提取切片值的函數是從http://www.jkp-ads.com/articles/slicers05.asp採取:
Public Function GetSelectedSlicerItems(SlicerName As String) As String
Dim oSc As SlicerCache
Dim oSi As SlicerItem
Dim lCt As Long
On Error Resume Next
Set oSc = ThisWorkbook.SlicerCaches(SlicerName)
If Not oSc Is Nothing Then
For Each oSi In oSc.SlicerItems
If oSi.Selected Then
GetSelectedSlicerItems = GetSelectedSlicerItems & oSi.Name & ", "
lCt = lCt + 1
End If
Next
If Len(GetSelectedSlicerItems) > 0 Then
If lCt = oSc.SlicerItems.Count Then
GetSelectedSlicerItems = "maandag"
Else
GetSelectedSlicerItems = Left(GetSelectedSlicerItems, Len(GetSelectedSlicerItems) - 2)
End If
Else
GetSelectedSlicerItems = "No items selected"
End If
Else
GetSelectedSlicerItems = "No slicer with name '" & SlicerName & "' was found"
End If
End Function
感謝您的快速回復。我應該補充一點,我使用的是切片機,而不是數據透視表。因此,不幸的是,我無法報告連接。我需要表保留一個表,因爲用戶必須能夠更改數據。 –