+ Reply to Thread
Results 1 to 3 of 3

Assignments to Cells

  1. #1
    Valued Forum Contributor ffffloyd's Avatar
    Join Date
    05-20-2008
    Location
    Perth, WA, Australia
    MS-Off Ver
    Office 365
    Posts
    249

    Assignments to Cells

    It has been a while since I've used VBA and I'm wondering what I've forgotten.

    If I call a function from Excel with a range of cells, am I able to alter the values of those cells from within the function that I've written?

    Let's say I have the following function:

    Please Login or Register  to view this content.
    It is being called from cell P4 with the range of cells being B2:L9.

    Somewhere in there I have the following piece of code:

    Please Login or Register  to view this content.
    where iRow = 1 and jCol = 1. I can determine via debug messages that they correctly point to B2 and that prior to this assignment rng.Cells(iRow, jCol).Value = "H".

    However, when I try to step past the above statement in the debugger, it goes to Never Never Land (ie, becomes unresponsive). When I simply execute it from the worksheet without debugging, it returns #VALUE!.

    What obvious error am I making in trying to assign a value? Is there some switch I need to set first so that I can assign values to the passed range? I have tried passing it both ByVal and ByRef.
    Last edited by ffffloyd; 03-06-2010 at 01:27 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Assignments to Cells

    If I read that correctly, you're trying to change the value in B2 using the function you've written and inserted into cell P4? If so, no dice.

    Functions are able to do any sort of calculation and loop and search and run all over the workbook, but the only thing the function can RETURN to the worksheet is a value into the cell holding the function as a formula.

    You will have to construct a normal macro to change the values in your RNG based on a string you enter in P4...or something like that.

    So, enter a formula in P4...get an answer in P4, nothing else.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Valued Forum Contributor ffffloyd's Avatar
    Join Date
    05-20-2008
    Location
    Perth, WA, Australia
    MS-Off Ver
    Office 365
    Posts
    249

    Re: Assignments to Cells

    Thanks JB. I'm going to have to skin, slice and dice this cat a different way.


    Disclaimer:
    Apologies to cat lovers. No animals will, in fact, be harmed in the resolution of this problem.

+ 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