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

Dman333

New Member
Joined
Sep 30, 2016
Messages
34
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
 

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,615
Hello.

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

Dman333

New Member
Joined
Sep 30, 2016
Messages
34
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
 

Dman333

New Member
Joined
Sep 30, 2016
Messages
34
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?
 

jolivanes

Well-known Member
Joined
Sep 5, 2004
Messages
1,539
Office Version
2013, 2007
Platform
Windows
What happens if you would change that to
Code:
Sheets("Sheet1").Calculate
'<---- or maybe Sheet2 or Sheet3 or whatever.
 

Dman333

New Member
Joined
Sep 30, 2016
Messages
34
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.
 

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,615
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:

Watch MrExcel Video

Forum statistics

Threads
1,102,150
Messages
5,485,052
Members
407,479
Latest member
jbone2020

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top