+ Reply to Thread
Results 1 to 9 of 9

Excel 2007 : VB module changes values on all sheets every time it runs

  1. #1
    Registered User
    Join Date
    10-16-2011
    Location
    texas
    MS-Off Ver
    Excel 2003
    Posts
    28

    VB module changes values on all sheets every time it runs

    I have the following module:
    Please Login or Register  to view this content.
    this is designed to give me a total count of worksheets in a workbook.

    The problem is that I have multiple workbooks using this command, and I've realized now that when I use it on one workbook, it takes those values and places them in ALL workbooks, which is causing inaccurate results to be displayed everywhere it's used EXCEPT whatever sheet I'm currently working on (as it does the formula for that sheet)

    for example:
    I create workbook 1. when done, it has 5 worksheets total. I put in the formula where I want it and it displays (accurately) "5"

    I then create workbook 2 while workbook 1 is still open. Workbook 2 has 3 sheets. When done creating and filling it with info, I put the formula where I want it in workbook 2. It calculates "3".

    the issue, is it takes that "3" and plugs it into both workbook 1 and workbook 2's cells that contain the shtcnt() function.

    The end result is worksheet 2 has the correct information, but workbook 1 now displays "3" instead of "5"

    How would i stop it from doing this? The only Idea I have is to create a "shtcnt1()" "shtcnt2" etc etc for every worksheet I create, but that's not very practical
    Last edited by aff219; 11-01-2011 at 11:45 AM.

  2. #2
    Registered User
    Join Date
    10-16-2011
    Location
    texas
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: VB module changes values on all sheets every time it runs

    no one? =/

  3. #3
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VB module changes values on all sheets every time it runs

    aff219;
    Your function uses "ActiveWorkbook". There is only 1 ActiveWorkbook at a time. So ALL open workbooks will get the exact same answer from that function at any time. If you go back to workbook1 and cause it to calculate, you will then see that workbook 2 has 5.

    You need to change your function to :
    Please Login or Register  to view this content.
    Then in your worksheet enter "=ShtCnt(A1)" (any cell address will do).
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  4. #4
    Registered User
    Join Date
    10-16-2011
    Location
    texas
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: VB module changes values on all sheets every time it runs

    Quote Originally Posted by foxguy View Post
    aff219;
    Your function uses "ActiveWorkbook". There is only 1 ActiveWorkbook at a time. So ALL open workbooks will get the exact same answer from that function at any time. If you go back to workbook1 and cause it to calculate, you will then see that workbook 2 has 5.

    You need to change your function to :
    Please Login or Register  to view this content.
    Then in your worksheet enter "=ShtCnt(A1)" (any cell address will do).
    thanks, might i ask what the A1 part does?

    *edit*

    when i use it I seem to get #VALUE! error
    Last edited by aff219; 10-28-2011 at 05:59 PM.

  5. #5
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VB module changes values on all sheets every time it runs

    The A1 is just any cell address. You could use Z69 or anything else.
    I don't know how to pass a workbook to the Function. So I pass a range (any cell) and let the Function figure out what workbook it is in.

  6. #6
    Registered User
    Join Date
    10-16-2011
    Location
    texas
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: VB module changes values on all sheets every time it runs

    Quote Originally Posted by foxguy View Post
    The A1 is just any cell address. You could use Z69 or anything else.
    I don't know how to pass a workbook to the Function. So I pass a range (any cell) and let the Function figure out what workbook it is in.
    anyoen able to help with the VALUE problem?

  7. #7
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VB module changes values on all sheets every time it runs

    It works fine on my computer.
    Do you know how to debug a macro?

    If you don't, make a new workbook and put the function in it and try to use it in any worksheet. Make sure the error occurs then upload the workbook and I'll take a look.

  8. #8
    Registered User
    Join Date
    10-16-2011
    Location
    texas
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: VB module changes values on all sheets every time it runs

    Quote Originally Posted by foxguy View Post
    It works fine on my computer.
    Do you know how to debug a macro?

    If you don't, make a new workbook and put the function in it and try to use it in any worksheet. Make sure the error occurs then upload the workbook and I'll take a look.
    Thanks for the help. I'm an idiot lol =/

    having me make a new worksheet with the formula made me realize my mistake, i only plugged the new code into one of the worksheets (I currently have 7) so only one was working properly.

    thanks

  9. #9
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VB module changes values on all sheets every time it runs

    If you put the code into a Standard module instead of a Sheet module it will be available to all worksheets. Code in a Sheet module is only available to that theet.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1