Results 1 to 8 of 8

Thread: How to pull a sheet name from a cell and use to calculate just one sheet in vba
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Sep 2016
    Posts
    32
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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

  2. #2
    Board Regular
    Join Date
    Aug 2003
    Location
    England
    Posts
    5,615
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    2 Thread(s)

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

    Hello.

    Can you temporarily copy that one sheet to a new workbook, do your calculation, then copy the data back?
    -------------------------
    Hope this is helpful.
    -------------------------

    Have been away from the forum for quite a while, but am now back.

  3. #3
    New Member
    Join Date
    Sep 2016
    Posts
    32
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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

  4. #4
    Board Regular
    Join Date
    Sep 2004
    Posts
    1,389
    Post Thanks / Like
    Mentioned
    9 Post(s)
    Tagged
    0 Thread(s)

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

    Before you calculate you're one sheet, set the calculation to manual, calculate the sheet and after that set the calculation to automatic again.

    https://www.howtogeek.com/252463/how...heet-in-excel/

    Code:
        Application.Calculation = xlManual
    
    
        ActiveSheet.Calculate
    
    
        Application.Calculation = xlAutomatic
    Theory is when you know something, but it doesnít work. Practice is when something works, but you donít know why. Politicians combine theory and practice: nothing works and they donít know why.

  5. #5
    New Member
    Join Date
    Sep 2016
    Posts
    32
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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?

  6. #6
    Board Regular
    Join Date
    Sep 2004
    Posts
    1,389
    Post Thanks / Like
    Mentioned
    9 Post(s)
    Tagged
    0 Thread(s)

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

    What happens if you would change that to
    Code:
    Sheets("Sheet1").Calculate
    '<---- or maybe Sheet2 or Sheet3 or whatever.
    Theory is when you know something, but it doesnít work. Practice is when something works, but you donít know why. Politicians combine theory and practice: nothing works and they donít know why.

  7. #7
    New Member
    Join Date
    Sep 2016
    Posts
    32
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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.

  8. #8
    Board Regular
    Join Date
    Aug 2003
    Location
    England
    Posts
    5,615
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    2 Thread(s)

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

    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 by onlyadrafter; Sep 9th, 2019 at 09:12 AM.
    -------------------------
    Hope this is helpful.
    -------------------------

    Have been away from the forum for quite a while, but am now back.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •