+ Reply to Thread
Results 1 to 12 of 12

User-defined range is called fine in a Workbook sub, but not a module sub?

  1. #1
    Registered User
    Join Date
    04-27-2022
    Location
    New York, USA
    MS-Off Ver
    Office 365 Apps for Enterprise
    Posts
    9

    Question User-defined range is called fine in a Workbook sub, but not a module sub?

    I'd like to preface that yes, I am new, but I am also not looking to dive super deep into this language. I'm just trying to get this one book setup for now... This is also my first post & day here, so hello!

    I have a defined range in a "Form" sheet, and I even did the "option explicit" thing and set it as
    Please Login or Register  to view this content.
    Set to:
    Please Login or Register  to view this content.
    "notebx_label" and "Note" are cells I named.
    I've called it in the "ThisWorkbook" section under a general "Workbook_Open()" public sub to set the text of the cells to white (to hide them without hiding the entire column) as such:

    Please Login or Register  to view this content.
    and it works fine!

    But in a module on its own that I also have set to a Public sub, I called it the same way:

    Please Login or Register  to view this content.
    and it gives me an error for "1004 - method 'range' of object '_global' failed"

    How is it different? It's called in the workbook and it works just fine! why doesn't it work in the module? I have everything set to public etc. in an attempt to get everything called properly.

    Not sure how to insert the whole code, but I did pictures:

    ExcelVBA1.png
    ExcelVBA1.5_.png
    ExcelVBA2.png

  2. #2
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: User-defined range is called fine in a Workbook sub, but not a module sub?

    on your sub "AddNote"

    call the cell name:

    Please Login or Register  to view this content.
    the cell is already a "range" so adding range.range is confusing excel.

  3. #3
    Registered User
    Join Date
    04-27-2022
    Location
    New York, USA
    MS-Off Ver
    Office 365 Apps for Enterprise
    Posts
    9

    Re: User-defined range is called fine in a Workbook sub, but not a module sub?

    Thank you for explaining why ! But now I'm getting a different error;

    "Object variable or With block variable not set"

  4. #4
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: User-defined range is called fine in a Workbook sub, but not a module sub?

    Make sure the name of the cell inside the brackets is exactly what is named.
    Excel is looking for the object in the brackets, but it can't find it.

  5. #5
    Registered User
    Join Date
    04-27-2022
    Location
    New York, USA
    MS-Off Ver
    Office 365 Apps for Enterprise
    Posts
    9

    Re: User-defined range is called fine in a Workbook sub, but not a module sub?

    Yea, I pasted it exactly:
    Please Login or Register  to view this content.
    is it something to do with how I have it created?

    The cell name "notebx_label" is I2 (i2), and "Note" is a merged cell of I3 thru I8.

    Attachment 778478

  6. #6
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: User-defined range is called fine in a Workbook sub, but not a module sub?

    Any chance you can upload your document here? Someone here said a picture is worth a thousand words, and a workbook is worth a thousand pictures.

    VBA doesn't like merged cells; that could be a reason

    the syntax for calling a named cell is :


    Sheet#.[CELL NAME]

    Sheet# is the absolute reference of a sheet and not the user-defined name.

    I could diagnose the problem in a couple of minutes with the workbook.

  7. #7
    Registered User
    Join Date
    04-27-2022
    Location
    New York, USA
    MS-Off Ver
    Office 365 Apps for Enterprise
    Posts
    9

    Post Re: User-defined range is called fine in a Workbook sub, but not a module sub?

    Ok, I think I attached it.
    Attached Files Attached Files

  8. #8
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: User-defined range is called fine in a Workbook sub, but not a module sub?

    I see the problem.

    1st issue:

    change Sheets("Form") ->worksheets("Form") or to ->Sheet4

    2nd issue:

    note_rng is a variable and not a cell reference (it's a variable referencing cells)

    to change the font color to black in your other module, I would use this code (below) instead of the one we wrote earlier:

    Please Login or Register  to view this content.

  9. #9
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: User-defined range is called fine in a Workbook sub, but not a module sub?

    please note if the cell is LOCKED, you will get the application-defined error! (your submit code attempts to change the properties of the cell while it's locked*, giving you the error)
    Last edited by carlmon; 04-27-2022 at 04:06 PM.

  10. #10
    Registered User
    Join Date
    04-27-2022
    Location
    New York, USA
    MS-Off Ver
    Office 365 Apps for Enterprise
    Posts
    9

    Thumbs up Re: User-defined range is called fine in a Workbook sub, but not a module sub?

    With this line I'm getting "Application-defined or object-defined error"

    Please Login or Register  to view this content.
    I have it exactly the same as you typed it, inside the module.

    EDIT: Sorry, I posted this before I saw the one you added about this error, thank you!
    Last edited by Faru298; 04-27-2022 at 04:04 PM.

  11. #11
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: User-defined range is called fine in a Workbook sub, but not a module sub?

    Quote Originally Posted by Faru298 View Post
    With this line I'm getting "Application-defined or object-defined error"

    Please Login or Register  to view this content.
    I have it exactly the same as you typed it, inside the module.

    EDIT: Sorry, I posted this before I saw the one you added about this error, thank you!
    I was scratching my head for a solid 20 minutes trying to figure out the error . I'm glad we got your issue resolved thx for the rep!

  12. #12
    Registered User
    Join Date
    04-27-2022
    Location
    New York, USA
    MS-Off Ver
    Office 365 Apps for Enterprise
    Posts
    9

    Re: User-defined range is called fine in a Workbook sub, but not a module sub?

    Thank you so much! All errors are clear and everything is working as I wanted! Amazing :D

    A week ago I had NO idea Excel could be setup to do things like this, as a programming nerd this is so cool.

    many thanks!

+ 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. [SOLVED] Copy data from another workbook based on user defined range
    By winfs in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-23-2022, 09:33 AM
  2. User-defined type not defined when copying macro from one workbook to another
    By Mogles in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-01-2018, 08:08 AM
  3. Macro problem - loops theough user defined functions not called in macro
    By beccyclaire in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-04-2016, 02:09 AM
  4. [SOLVED] Determine what called a user defined function?
    By brucemc777 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-03-2016, 03:23 PM
  5. User Defined function getting called for all the formula calculation cells
    By 1man in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-13-2014, 08:36 AM
  6. how to make a function defined in one workbook being called by other workbooks
    By anshulbansal in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-11-2008, 11:05 AM
  7. User-defined type collection in class module
    By Rob in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-31-2006, 01:10 PM

Tags for this Thread

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