+ Reply to Thread
Results 1 to 12 of 12

Using a named range in VBA code

  1. #1
    Registered User
    Join Date
    02-25-2010
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    8

    Using a named range in VBA code

    I am trying to write a simply code to copy some formulas down to the last row of newly imported data. I have a UDF named Lastrow() that calculates the last row of the new data. The formula is in a cell named FinalRow.

    I have tried numerous ways of referencing the value of FinalRow with no luck. Below is my latest attempt that hopefully is clear enough that it completes the idea that I am working on.
    Please Login or Register  to view this content.
    Any help is appreciated.
    Last edited by Paul; 02-25-2010 at 01:16 AM. Reason: Added code tags for user;

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,936

    Re: Using a named range in VBA code


    I tried your code in a standard sheet module with no problem. What module is the code in?
    What sheet does: valFinalRow = Range("namedrange").Value refer to?
    Is it the same or different from the "Activesheet"?
    Ben Van Johnson

  3. #3
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Using a named range in VBA code

    Hi TripleC, welcome to the forum.

    Please take a moment to read the forum rules and in the future be sure to wrap your code in CODE tags as shown below. I've done it for you this time.

    Please Login or Register  to view this content.
    [/noparse][/CODE]

    Thank you.

  4. #4
    Registered User
    Join Date
    02-25-2010
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Using a named range in VBA code

    Thanks for the response. I have the code in a module not on the sheets code module; I probably should add an activesheet reference somewhere in there if I need this on another sheet too. However, I think I see what the problem is now. The LastRow() UDF seems to be reverting to a #value error. Here is that function:

    Please Login or Register  to view this content.
    I just now added the volatile declaration, it updated correctly. I ran the copy macro and it does work, however, the LastRow #value errored again. Probably not supposed to changed problems mid-thread, but that seems to be my problem now.
    Last edited by Paul; 02-25-2010 at 06:28 PM.

  5. #5
    Registered User
    Join Date
    02-25-2010
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Using a named range in VBA code

    Thanks, I saw your post after my reply, I will use the code formatting in the future.

  6. #6
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Using a named range in VBA code

    HI

    I wouldn't use this UDF - you don't need it as you can calculate the last row in your macro itself:

    Please Login or Register  to view this content.
    Richard Schollar
    Microsoft MVP - Excel

  7. #7
    Registered User
    Join Date
    02-25-2010
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Using a named range in VBA code

    Thanks for the reply.

    I actually use the last row calculation in other in-sheet formulas. I can see that if I was stronger with VBA I could probably avoid the UDF. However, I worry that my learning curve won't accommodate my due date for this file.

    I don't understand why it error's out.

  8. #8
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Using a named range in VBA code

    You shouldn't need a UDF to calculate the last row as you can, should you desire, use native Excel formulas to do so. The problem with the UDF as it stands is that it refers to the activesheet and not the sheet on which the formula resides. This means as you move around sheet sin your workbook, the UDF refers to these newly activated sheets which means the value it returns will vary depending on which sheet is active. It is asking for trouble and should be avoided.

  9. #9
    Registered User
    Join Date
    02-25-2010
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Using a named range in VBA code

    Thanks again. Very convincing, I will look into native excel solution for this.

  10. #10
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,936

    Re: Using a named range in VBA code


    Please post some code where LastRow is called.
    Also, your UDF does not declare its return value so that it defaults to Variant, it should be:
    Please Login or Register  to view this content.
    And, in the line
    Please Login or Register  to view this content.
    I'm not sure what sheet [A1] refers to.
    If you pass the sheet name as a parameter to a function that returns a LONG it would look like:
    Please Login or Register  to view this content.
    Finally, your use of the FIND function seems to indicate that the columns on any sheet may have different lengths. Is this so?
    Last edited by protonLeah; 02-25-2010 at 03:37 AM.

  11. #11
    Registered User
    Join Date
    02-25-2010
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Using a named range in VBA code

    I am just using the LastRow UDF as an in-cell function on a sheet. I then use that value for the original code referenced above and for multiple other standard excel functions on the same sheet as well as other Report sheets.

    The [A1] is a cell reference, or so I think. The UDF calculates correctly it seems. However, it does not always update or it errors out.

    The function is needed as the sheet receives weekly or monthly data updates.

    This is my first client project using VBA and I doubt that I can get it to the level of automation that I would like. At this point I am trying to append new data and copy cell formulas down. I'd eventually like to have the data scrubbing fairly automated too since many of the formulas identify rows with errors.

  12. #12
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,936

    Re: Using a named range in VBA code

    If I were you, I would replace all occurrences of the UDF with COUNT or COUNTA...

    But it is just as easy to find the last row directly in the VBA procedures with:
    Please Login or Register  to view this content.

+ 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