Consulting

Results 1 to 12 of 12

Thread: Solved: Convert a range from relative to absolute cell reference

  1. #1

    Solved: Convert a range from relative to absolute cell reference

    Is there a macro to convert a range of cells (say F4 to F144), linked to other cells in the workbook, from a relative to an absolute cell reference?

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I don't know of a simple VBA method, but give this code version a try.

    [vba]
    Revised code posted below[/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Thanks mdmackillop, I'll give it a try.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I notice it's not quite there, missing the $ in such as =%H3. Can you fix this yourself?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Revised code
    [VBA]
    Sub ConvertAbsolute()
    Dim F As String
    Dim Pos1 As Long, Pos2 As Long
    Dim Num As Long, Rw As Long, Cm As Long
    Dim Cel As Range
    Dim Tmp As Long
    For Each Cel In Selection
    F = Cel.FormulaR1C1
    F = Replace(F, "RC", "R[0]C")
    Debug.Print F
    Rw = Cel.Row
    Cm = Cel.Column
    Pos2 = 1
    Do
    Ln = Len(F)
    Pos1 = InStr(Pos2, F, "C[")
    If Pos1 = 0 Then GoTo DoRows
    Pos2 = InStr(Pos1, F, "]")
    Num = Mid(F, Pos1 + 2, Pos2 - Pos1 - 2)
    F = Left(F, Pos1 + 1) & CStr(Num + Cm) & Right(F, Ln - Pos2 + 1)
    Loop
    DoRows:
    Pos2 = 1
    Do
    Ln = Len(F)
    Pos1 = InStr(Pos2, F, "R[")
    If Pos1 = 0 Then GoTo Exits
    Pos2 = InStr(Pos1, F, "]")
    Num = Mid(F, Pos1 + 2, Pos2 - Pos1 - 2)
    F = Left(F, Pos1 + 1) & CStr(Num + Rw) & Right(F, Ln - Pos2 + 1)
    Loop
    Exits:
    F = Replace(F, "[", "")
    F = Replace(F, "]", "")
    F = Replace(F, "RC", "R1C")
    Cel = F
    Debug.Print F
    Next
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Thanks again! Am I supposed to run the macro after I highlight the range that I want to convert from relative to absolute cell reference?

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    That's the way it's coded, but easily changed to loop through any defined range or row/column headed by the active cell. If you need help, let us know.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub xxConvertAbsolute()
    Dim cell As Range

    For Each cell In Selection
    If cell.HasFormula Then
    cell.Formula = Application.ConvertFormula( _
    cell.Formula, xlA1, xlA1, xlAbsolute)
    End If

    Next cell

    End Sub
    [/vba]

  9. #9
    I tried xld's code first because it is so compact and it worked perfectly. Thanks guys!!

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Yeah!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    If I knew this would upset you, I would have been more diplomatic

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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