我想只要編輯匿名類型的答案,但我的編輯遭到拒絕這樣做新的答案。他錯過了RefreshTeamQuery方法中的部分代碼,如鏈接文章所示(here是更直接鏈接到原始代碼)。
我還有問題從工作簿打開事件調用這個問題,因爲我不認爲這些按鈕是在工具欄中創建的,或者在打開wookbook時以某種方式鏈接到工作表。使用按鈕上的代碼雖然工作正常。
Private Function FindTeamControl(tagName As String) As CommandBarControl
Dim commandBar As commandBar
Dim teamCommandBar As commandBar
Dim control As CommandBarControl
For Each commandBar In Application.CommandBars
If commandBar.Name = "Team" Then
Set teamCommandBar = commandBar
Exit For
End If
Next
If Not teamCommandBar Is Nothing Then
For Each control In teamCommandBar.Controls
If InStr(1, control.Tag, tagName) Then
Set FindTeamControl = control
Exit Function
End If
Next
End If
End Function
Sub RefreshTeamQuery(shtTFSExcel_Name As String) '(rangeName As String)
Dim activeSheet As Worksheet
Dim teamQueryRange As Range
Dim refreshControl As CommandBarControl
Set refreshControl = FindTeamControl("IDC_REFRESH")
If refreshControl Is Nothing Then
MsgBox "Could not find Team Foundation commands in Ribbon. Please make sure that the Team Foundation Excel plugin is installed.", vbCritical
Exit Sub
End If
'Disable screen updating temporarily so that the user doesn’t see us selecting a range
Application.ScreenUpdating = False
'Capture the currently active sheet, we will need it later
Set activeSheet = ActiveWorkbook.activeSheet
Set teamQueryRange = Worksheets(shtTFSExcel_Name).ListObjects(1).Range
teamQueryRange.Worksheet.Select
teamQueryRange.Select
refreshControl.Execute
activeSheet.Select
Application.ScreenUpdating = True
End Sub
這絕對是正確的答案。像魅力一樣工作!感謝匿名類型。 – kroonwijk
我的目標是取悅 –
部分代碼從RefreshTeamQuery方法中丟失。我試圖編輯答案,但被拒絕,所以用下面的完整代碼做了一個新的答案。 –