Excel VBA Recalculate Selection

白昼怎懂夜的黑 提交于 2019-12-05 21:43:38

You should be able to use the following:

Public Sub RecalculateSelection()
    Dim rng As Range
    Set rng = Application.Selection
    rng.Calculate
End Sub

You should place some error handling around the 'Set rng' line, as the user may not have selected a range (e.g. they may have selected a chart).

By using the application object you don't need to capture the Workbook_SheetSelectionChange event.

if your using the method from the accepted answer above you should first check that...

If Not Selection Is Nothing Then
  If TypeName(Application.Selection) = "Range" Then 
  Dim Rng As Range 
  Set Rng = Application.Selection 
  Rng.Calculate 
 End If 
End If 

Also you might like to add it to the sheet and sheetname context menus. The name of the two commandbars that you need to do that are... "Cell" and "Ply"

If you just want to recalculate the currently selected cells, ignoring cells that are dependent on them you can use my RangeCalc addin, downloadable from http://www.decisionmodels.com/downloads.htm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!