+ Reply to Thread
Results 1 to 3 of 3

Writing a Function

Hybrid View

svaiskau Writing a Function 06-14-2007, 02:00 PM
jasoncw Use Worksheets("Sheet... 06-14-2007, 02:03 PM
MrShorty many say that it is bad... 06-14-2007, 06:23 PM
  1. #1
    Registered User
    Join Date
    06-06-2007
    Posts
    53

    Writing a Function

    I'm working on a function that will compare each cell in a range to the cell before it, and return a 0 if they are equal and a 1 if they are different.

    Know how to make the loop to do the comparison, but I am having an issue with using cells on other worksheets. In subroutines, I can use Worksheets("Worksheet Name").Select to change worksheets and then pull data. When I write a function, I cannot change worksheets (even Worksheets("Sheet Name").Cells() doesn't work.)

    What's the trick here? What's the command to change worksheets so I can pull cell data from other sheets for a user-defined function, or is that not possible?

  2. #2
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Use
    Worksheets("Sheet Name").Range("A1")
    No need to select.

    HTH

    Jason

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,436
    many say that it is bad practice to access information directly from within a UDF like that. Better might be to pass the range to the function via the argument list. To illustrate
    function compare1(range1, range2)
    compare1=0
    If range1.cells(1,1).value=range2.cells(1,1).value then compare1=1
    end function
    Then, when you call the function from the spreadsheet, you just reference the range from the other worksheet just like in any other built in function. (=compare1(sheet1!a1,sheet2!a2))

+ 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