How to pull a sheet name from a cell and use to calculate just one sheet in vba

Dman333

Board Regular
Joined
Sep 30, 2016
Messages
68
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a large workbook that has a sheet which calculates results with and without inflation. I have code that changes inflation to 0 and then back to what it was originally. Before I copy the original assumptions back I need to calculate just one sheet that is selected in a drop-down box in a sheet. That way I can copy and paste values to the output report. Everything works fine except I can't get it to calculate just the selected sheet. The only way I've been able to get it to work is by calculating the entire workbook which takes some time.

So, I need a way to pull the sheet name from a cell and use it to calculate just that one sheet. Below is an unsuccessful attempt I took at doing that.

Any help is greatly appreciated as I'm a novice in vba.

Cheers - Dman333


Private Sub CommandButton1_Click()


Dim ws As Worksheet
Set ws = Worksheets("Prof by Pl (no infl)").Range("A1")

ws.calculate
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hello.

Can you temporarily copy that one sheet to a new workbook, do your calculation, then copy the data back?
 
Upvote 0
Hi,

Interesting idea, but I think the sheet is too big and I would be concerned about formula references etc. I was hoping for a more stable solution. Originally, I thought I could use the Indirect command in VBA to set a variable and then us that to bring in the sheet name but couldn't get it to work.

Thanks for the response though.
Cheers
 
Upvote 0
Thanks for taking the time to respond.

But the whole issue is the ActiveSheet isn't the sheet that needs to be recalculated. The ActiveSheet is the report that is updated using the Sheet Name as text from a drop down box which drives the reports indirect formulas. So, you change the sheet in the drop down box - hit a command button that launches the VBA code which changes the inflation assumption to 0. This is when I need the selected sheet from the drop down box to recalculate so the report doesn't include inflation. Then the inflation assumptions are copied back to their original place and I need to recalculate just the chosen sheet again. It works now, but only with calculating the entire workbook which is slow.

Any ideas out there?
 
Upvote 0
What happens if you would change that to
Code:
Sheets("Sheet1").Calculate
'<---- or maybe Sheet2 or Sheet3 or whatever.
 
Upvote 0
I don't think I explained the problem clearly. The code above would work great except that "Sheet1" could be any one of 28 sheets. What I need is to able to have that sheetname reference be a variable and then be able to set the variable to be the contents of a cell. That's the core question I have.

Thanks for your time.
 
Upvote 0
Hello,

In that case, does

Code:
    Application.Calculation = xlManual
    MY_CALC_SHEET = Sheets("Sheet3").Range("A1").Value
    With Sheets(MY_CALC_SHEET)
        .Calculate
    End With
    Application.Calculation = xlAutomatic

is this what you require? You will need to change the variable location to suit.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top