Change Cell Reference between Absolute and Relative

后端 未结 2 2032
眼角桃花
眼角桃花 2021-01-07 11:03

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

相关标签:
2条回答
  • 2021-01-07 11:19

    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

    0 讨论(0)
  • 2021-01-07 11:29

    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)
    
    0 讨论(0)
提交回复
热议问题