Change Cell Reference between Absolute and Relative

爷,独闯天下 提交于 2019-12-03 22:36:13

问题


I want to write a macro which goes through all cell references in a selection's cells formula and changes them to absolute or relative.

Is there a formatting variable which can change this or a function which does this already (similar to what pressing F4 does) but as a macro.


回答1:


You can use ConvertFormula method.

4th Parameter determines whether its absolute or not. 1 sets it to absolute and 4 sets it to relative. As per one comment to this answer, if you are looking for mixed references, then its bit complex. but reading your question and comments, I think that's not what you are after.

Examples:
'/ Set it to absolute
ActiveCell.Formula = Application.ConvertFormula(ActiveCell.Formula, xlA1, xlA1, 1)

'/ Set it to relative
ActiveCell.Formula = Application.ConvertFormula(ActiveCell.Formula, xlA1, xlA1, 4)



回答2:


I see you have edited the question but since I have already worked on this, I am posting an answer.

If you do not know what the formula contains and want to change Relative to Absolute and Absolute/Mixed to Relative then try this

Let's say I have 4 ranges in my Selection as shown below

So I can use RegEx as suggested Here to extract individial addresses and find what kind of formula is it and then do the changes as suggested by @cyboashu

Const sPattern As String = _
"(['].*?['!])?([[A-Z0-9_]+[!])?(\$?[A-Z]+\$?(\d)+(:\$?[A-Z]+\$?(\d)+)?|\$?[A-Z]+:\$?[A-Z]+|(\$?[A-Z]+\$?(\d)+))"

Sub Sample()
    Dim sMatches As Object, objRex As Object
    Dim rng As Range, aCell As Range
    Dim sFormula As String
    Dim bAbsMix As Boolean, bRel As Boolean

    Set rng = Selection

    Set objRex = CreateObject("VBScript.RegExp")

    With objRex
        .IgnoreCase = True
        .Global = True
    End With

    For Each aCell In rng
        objRex.Pattern = """.*?"""
        sFormula = aCell.Formula
        sFormula = objRex.Replace(sFormula, "")

        objRex.Pattern = "(([A-Z])+(\d)+)"
        objRex.Pattern = sPattern

        If objRex.test(sFormula) Then
            Set sMatches = objRex.Execute(sFormula)
            If sMatches.Count > 0 Then
                For Each Match In sMatches
                    If Len(Match) = Len(Replace(Match, "$", "")) Then
                        bRel = True
                    Else
                        bAbsMix = True
                    End If
                Next Match
            End If
        End If

        If bAbsMix = True Then  '<~~ It is Absolute/Mixed
            Debug.Print sFormula & " in " & aCell.Address & " is Absolute/Mixed"
            aCell.Formula = Application.ConvertFormula(aCell.Formula, xlA1, xlA1, 4)
        Else '<~ It is Relative
            Debug.Print sFormula & " in " & aCell.Address & " is Relative"
            aCell.Formula = Application.ConvertFormula(aCell.Formula, xlA1, xlA1, 1)
        End If

        bRel = False: bAbsMix = False
    Next aCell
End Sub

In Immediate Window



来源:https://stackoverflow.com/questions/38821608/change-cell-reference-between-absolute-and-relative

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!