tags:

views:

392

answers:

4

Using Application.CalculateFull() i can refresh entire worksheet. Is there a way to refresh only specific formulas in the worksheet and not the entire worksheet?

+1  A: 
Range("A3:C5").Calculate 'calculate all the cells from A3 to C5

OR

Range("A3").Calculate  'only calculate cell A3
shahkalpesh
Interesting. I haven't seen that before. I would imagine that Excel will calculate any cell that feeds into the calculation of the desired cells. So if A3 referred to A2, then A2 will also need to be calculated. Might also work for downstream calculations, but I'm not sure.
guitarthrower
shahkalpesh
A: 

Hey what i mean is i have developed an excel plugin.. It exposes 3 user defined formulas (x, y , z) to excel application. All that works fine.. Now i need to add a refresh button that when clicked will refresh only those (x, y and z) formulas in the worksheet. So in the button's click handler what code i need to write?? Writing Application.CalculateFull() refreshes all the formulas. Thats not what i want.

Rashmi
Rashmi, this forum does not work like other forums, which are usually a flowing list of responses. On Stack Overflow, you should edit your *original* answer to include any updates by making use of the 'edit' link. So you should use the 'edit' link to copy-paste in this post and then delete this post. This way there is only one place to read your original question and everyone else posts answers. Make sense?
Mike Rosenblum
A: 

what i mean is i have developed an excel plugin.. It exposes 3 user defined formulas (x, y , z) to excel application. All that works fine.. Now i need to add a refresh button that when clicked will refresh only those (x, y and z) formulas in the worksheet. So in the button's click handler what code i need to write?? Writing Application.CalculateFull() refreshes all the formulas. Thats not what i want.

My top suggestion to achieve this would be to do a Find/Replace of the name of your user defined function (UDF) across the entire workbook. For example, if your UDF were named "MyFunction" you would want to replace "MyFunction(" with "MyFunction(".

Assuming that your Excel.Application reference were named 'excelApp', code to do a find/replace across all workbooks using C# could look as follows:

foreach (Excel.Workbook workbook in excelApp.Workbooks)
{
    foreach (Excel.Worksheet worksheet in workbook.Worksheets)
    {
        worksheet.Cells.Replace(
            "MyFunction(",
            "MyFunction(",
            Excel.XlLookAt.xlPart,
            Excel.XlSearchOrder.xlByRows,
            false,
            Type.Missing,
            Type.Missing,
            Type.Missing);
    }
}

Other lesser ideas:

(a) Utilize a Real-Time Data (RTD) server. This is usually utilized for having data such as stock prices update frequently or continuously. This could also be used, however, to execute your "Refresh On Demand" approach. I personally think that this would be overkill in in this case, as Find/Replace works 100% perfectly and is far easier to implement, but RTD could definitely be used here. For an article on how to do this, see Building Excel Real-Time Data Components in Visual Basic .NET it's geared towards VB.NET, not C#, but the principles are identical.

(b) Add another parameter to your function MyFunction(x, y, z, Refresh). By having each function reference the 'Refresh' cell, which can be placed on a hidden worksheet or hidden workbook, you can change the 'Refresh' cell to force all your functions to recalculate. The downside is that it requires this extra, dummy parameter.

(c) Stick with Application.CalculateFull(), as this is by far the easiest to code. I understand the performance issues, but it is extremely easy to code (just one line) and is 100% reliable.

Overall, I think that I would go with the Find/Replace approach. It is a clean, simple, and very effective technique for selective recalculations.

-- Mike

Mike Rosenblum
A: 

Thanks a lot for your answer. The find/replace one seems just fine for my purpose.

Rashmi
Rashmi, glad it works for you. :-) (You should mark my answer as the correct one and/or vote my answer up, by the way.)
Mike Rosenblum