+ Reply to Thread
Results 1 to 6 of 6

Function results incorrectly applied to multiple worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    05-21-2023
    Location
    Vancouver, BC
    MS-Off Ver
    2016
    Posts
    1

    Function results incorrectly applied to multiple worksheets

    Hi,

    I have some worksheets with data that I've created pivot tables for and do some more calculations
    on the results of the pivot tables (columns to the right of the generated pivot table). Each data set is on its
    own worksheet and has it's own pivot table in a separate worksheet.

    I created a function that takes as parameter a cell (column A) and then returns the previous row
    (in the column) with a non-blank value. The pivot tables look like this and calling the function
    in rows 4, 3, 2, 1 with parameter A4, A3, A2, A1 all return the value in A1.


    A1 - B1 - C1
    - B2 - C2
    - B3 - C3
    - C4


    The problem is that the last calculated results of the function are being applied to multiple worksheets (same row/column but different worksheet).
    If I go to the worksheet with PT1 results and update those columns right of the PT, save/close,
    reopen Excel and go to the worksheet with PT2 results I see that the results of the function from PT1.

    Can anyone tell me how to limit the scope of the results to their respective worksheet?

    Here's my function.
    Function PT_Site_Name(Cell As Range)
        Dim r As Range
            Set r = Cell
            Set r = ActiveSheet.Range(Cells(1, r.Column), Cells(r.Row, r.Column)).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
            PT_Site_Name = r.Value
    End Function
    Update: I created a simplified and reduced in size spreadsheet having the issue (to upload) but had the issue only once. I cannot reliably/consistently reproduce the problem and will park this until I can provide clear/simple steps to reproduce.

    Thanks,
    Darren
    Last edited by darrenkinley; 05-22-2023 at 10:30 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,440

    Re: Function results incorrectly applied to multiple worksheets

    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,743

    Re: Function results incorrectly applied to multiple worksheets

    Code Tags Added
    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)

    However, if you continue to not use code tags, you can expect to have your thread BLOCKED until you add them yourself.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  4. #4
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,037

    Re: Function results incorrectly applied to multiple worksheets

    Maybe try:

    Function PT_Site_Name(Cell As Range)
        Dim r As Range
        Set r = Worksheets(Cell.Parent.Name).Range(Cells(1, Cell.Column), Cells(Cell.Row, Cell.Column)).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        PT_Site_Name = r.Value
    End Function

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,997

    Re: Function results incorrectly applied to multiple worksheets

    BTW, you can use cell.worksheet rather than worksheets(cell.parent.name). You'll also need to qualify the Cells calls with the same sheet.
    Everyone who confuses correlation and causation ends up dead.

  6. #6
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,037

    Re: Function results incorrectly applied to multiple worksheets

    Thanks Rory
    Adjusted the code.
    Function PT_Site_Name(Cell As Range)
        Dim r As Range
        With Cell.Worksheet
            Set r = .Range(.Cells(1, Cell.Column), .Cells(Cell.Row, Cell.Column)).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        End With
        PT_Site_Name = r.Value
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 6
    Last Post: 02-16-2023, 02:39 PM
  2. Replies: 0
    Last Post: 11-03-2022, 04:51 PM
  3. Limit Variable Scope To By Value; Not By Reference.
    By Dal123 in forum Access Tables & Databases
    Replies: 5
    Last Post: 10-11-2021, 02:37 PM
  4. limit filter results count or paste results in range
    By incobart in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-28-2017, 06:33 AM
  5. Limit scope of Rows(x).Copy to a specfic # of columns
    By PoggiPJ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-14-2013, 01:15 PM
  6. Counting items in other worksheets and placing those results in a single worksheet
    By rkapadia16 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-02-2009, 07:29 PM
  7. Limit Find to a single column
    By merritts in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-05-2006, 03:25 PM

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