+ Reply to Thread
Results 1 to 3 of 3

VBA Functions - passing cell references into the VBA code

  1. #1
    Registered User
    Join Date
    04-06-2005
    Posts
    2

    VBA Functions - passing cell references into the VBA code

    Is it possible to pass a cell reference into a subroutine/function?
    For example if i create a VBA function, with an argument X, in the code for that function can I determine what the cell location is for X? I've been looking at the online help at the byval and byref options for function arguments, but I don't think they do what I need.

    As an (overly simplified) example of what I might want to do with this, I'd like a function that has one variable, that adds the value in the cell passed to it to the cell above. I could do this by passing the two values to the function, but I'd like to be able to do it by passing just the lower cell reference, and it working out which the cell is above.

    Pete.

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Pete

    here is an example
    this will work for a single cell or a range.


    This can be used as either a workshhet formula or as a maro function.

    if you want to use it as a macro funtion then you will need code in another macro
    Result = MyFunction("a1")



    Function MyFunction(MyCells As Range) As String
    Dim Rng As Range
    For Each Rng In MyCells
    MyFunction = MyFunction + Rng.Value
    next rng
    End Function

  3. #3
    Registered User
    Join Date
    04-06-2005
    Posts
    2

    Thumbs up

    Thanks mudraker !

    Pete.

+ 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