+ Reply to Thread
Results 1 to 8 of 8

#NAME? error when passing text in VBA script.

  1. #1
    Registered User
    Join Date
    07-31-2013
    Location
    Nashville
    MS-Off Ver
    Excel 2010
    Posts
    13

    #NAME? error when passing text in VBA script.

    Hello, thanks to this forum I've gotten started in my first VBA script.

    My long line that returns a #NAME? error is one that should return a date in a cell formatted as a date. Everything works except when I want to pass a SPACE character for failing a logical test. My original Excel calculation works fine when I pass " " but in the VBA it fails with a generic error. OK, it works when I put in an arbitrary number but that's not useful. I quickly changed it to ' ' since the entire calc is enclosed in double quotes which should have worked.

    I've opted to putting in Chr(32) and it didn't error out except for the #NAME error. It seems to pass this value on failing tests.

    I've been Googling for context and answers and it's not clear to me which way I should go. Does this make sense to you why it's happening? thanx, sam
    Last edited by sam452; 11-18-2013 at 12:47 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,369

    Re: #NAME? error when passing text in VBA script.

    It would probably help a lot if you shared the code and the formula that you hope to get out of it. It's probably as simple as doubling up the quotes.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    07-31-2013
    Location
    Nashville
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: #NAME? error when passing text in VBA script.

    Sure, I didn't because it's really long and everything up to the end seems to work, sam

    ActiveCell.Offset(row_number, 7).FormulaR1C1 = "=
    IF(RC[-6]=
    MEDIAN(RC[-6],
    INT(RC[-6])+'H:\Diggi Reports\[support values.xlsx]Constants'!R2C2,
    INT(RC[-6])+1),RC[-1],
    IF(RC[-6]=
    MEDIAN(RC[-6],
    INT(RC[-6]),
    INT(RC[-6])+'H:\Diggi Reports\[support values.xlsx]Constants'!R3C2),
    RC[-1],
    Chr(32)
    )
    )"

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,369

    Re: #NAME? error when passing text in VBA script.

    OK, tell me what the formula should look like when it gets there safely ... and what cell you are trying to put it in.


    Regards, TMS

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,369

    Re: #NAME? error when passing text in VBA script.

    Never mind ...

    Please Login or Register  to view this content.

    Regards, TMS

  6. #6
    Registered User
    Join Date
    07-31-2013
    Location
    Nashville
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: #NAME? error when passing text in VBA script.

    This is extremely helpful, thank you. The first general solution works and it makes sense to break down the complex into smaller, quoted chunks. And it appears you're using the CHAR calculation instead of the CHR I had found.

    Pasting it I find that the underscore character after the ampersand upset the VBA compiler. Is that a convention to use that character? And it's helpful to see other code improvements I can make as well, thanx, sam

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,369

    Re: #NAME? error when passing text in VBA script.

    And it appears you're using the CHAR calculation instead of the CHR I had found.
    CHAR in an Excel worksheet formula, Chr in VBA. For example, =CHAR(65) = "A" (no quotes) in Excel. ?chr(65) = "A" (no quotes) in VBA Immediate Window.

    You use an underscore (_) as a continuation character. This allows you to split long lines of code across several lines to make it more readable. But you may then need ampersand (&) to concatenate the string segments.

    So, if you had:
    Please Login or Register  to view this content.
    you could split that as
    Please Login or Register  to view this content.
    on the same line. And then, you could say:

    Please Login or Register  to view this content.

    It helps to be able to see how the various parts of the code or formulae line up.


    The other options were just variations on a theme. If cell A1 is selected, using ActiveCell is the same as saying Range("A1"). The difference is that, with ActiveCell, you have no idea which cell is selected ... unless it is selected somewhere nearby.

    It's better to control which cells you are accessing specifically rather than relying on the ActiveCell being where you think it should be. And, if you don't select cells, the ActiveCell won't change.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,369

    Re: #NAME? error when passing text in VBA script.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ 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. Replies: 2
    Last Post: 10-13-2011, 11:44 AM
  2. Script that edits the text printed on the button that runs the script
    By petalred in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-18-2008, 02:41 PM
  3. error passing a formula to a cell from a string
    By TheIrishThug in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-22-2005, 11:54 AM
  4. Error with passing collection as parameter
    By McManCSU in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-01-2005, 02:05 PM
  5. Passing variable from userform to module error
    By mworth01 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-05-2005, 12:47 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