Consulting

Results 1 to 7 of 7

Thread: Range("R1C1") gives error

  1. #1

    Range("R1C1") gives error

    Simple question: I want to address a cell by its R1C1 address (given as a string). I would have thought that should be easy with Range("R1C1"), but that fails with the error
    Run-time error '1004':
    Application-defined or object-defined error
    By contrast, Range("A1") works nicely. So, apparently I need to tell VBA that "R1C1" is an R1C1 address. How to do that?

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    An ugly way


    Sub drv()
    
    
        Application.ReferenceStyle = xlR1C1
        MsgBox [R1C1].Value
        Application.ReferenceStyle = xlA1
    
    
    
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Quote Originally Posted by Paul_Hossler View Post
    An ugly way
    In principle, that isn't ugly at all; certainly not in my case: I should have used Application.ReferenceStyle = xlR1C1 all along, since I don't normally use A1 format.

    However, sadly, if I replace the brackets with
    Range() to accommodate a variable, it gives a similar error:
    Sub Display_Value(address_in_R1C1_format As String)
        Dim previous_setting
        previous_setting = Application.ReferenceStyle
        Application.ReferenceStyle = xlR1C1
        MsgBox Range(address_in_R1C1_format).Value
        Application.ReferenceStyle = previous_setting
    End Sub
    Run-time error '1004':
    Method 'Range' of object '_Global' failed
    (In this code example, to make this sub useful for anyone who wants to keep it independent of the format used elsewhere, I save Application.ReferenceStyle in a dummy variable, and reset it to the same setting afterwards.)
    Last edited by Sebastian H; 07-12-2021 at 09:51 AM. Reason: renaming sub: it displays the value, not the address

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I Set it into a Range to make it easier to reference / use and it's (probably) independent of reference style

    There's probably some cleanup that could be done depending on the use

    May not even need to mess with Application.ReferenceStyle


    Option Explicit
    
    Sub drv()
        Call Display_Value("R1C1")
    End Sub
    
    Sub Display_Value(address_in_R1C1_format As String)
        Dim previous_setting As XlReferenceStyle
        Dim r As Range
        
        previous_setting = Application.ReferenceStyle
        Application.ReferenceStyle = xlR1C1
        
        MsgBox Application.ConvertFormula(address_in_R1C1_format, xlR1C1, xlA1)
        Set r = Range(Application.ConvertFormula(address_in_R1C1_format, xlR1C1, xlA1))
        MsgBox r.Value
        
        Application.ReferenceStyle = previous_setting
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 07-12-2021 at 01:26 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Thanks, Paul – this solves it.

    And your hunch was right: It's independent of reference style and works regardless of the Application.ReferenceStyle setting.

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
        MsgBox ActiveCell.Address(, , 1) & vbLf & ActiveCell.Address(, , -4150) & vbLf & ActiveCell.Offset(5, 8).Address(, , 1) & vbLf & ActiveCell.Offset(5, 8).Address(, , -4150) & vbLf & Cells(10, 4).Address(, , 1) & vbLf & Cells(10, 4).Address(, , -4150)
    End Sub
    If you are used to R30C12 style, you'd better use cells(30,12).

  7. #7
    It's not a question of what I'm used to, but what's given. Given is a string.

    But no worries, the thread has already been solved.
    Last edited by Sebastian H; 08-24-2021 at 12:38 AM. Reason: grammar

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •