Excel VBA use localized RC formula for Conditional Formatting?

后端 未结 2 1671
半阙折子戏
半阙折子戏 2021-01-14 22:36

I\'m using polish Excel so \"R[-2]C\" is \"W[-2]C\".

In every place I tried VBA only accepted RC notation. Which is good since it will work regardless of language ve

2条回答
  •  独厮守ぢ
    2021-01-14 23:32

    In addition to Anonymous Type's answer, here's an actual implementation which you can use as a utility:

    ' Rewrites the given formula from English to the current locale.
    ' This can be useful when working with conditional formatting expressions,
    ' where Excel only accepts localized formulas for some reason.
    
    ' For this function to work, you must supply a cell which can be used for
    ' generating the formula. This cell will be cleared after the operation, so
    ' avoid cells which contain any meaningful data - use a temporary sheet if
    ' you already have one.
    Public Function ConvertToLocalizedFormula(formulaToConvert As String, _
                                              notationToUse As XlReferenceStyle, _
                                              ByRef tempCell As Range _
                                              ) As String
    
    
        If notationToUse = xlR1C1 Then
            tempCell.FormulaR1C1 = formulaToConvert
            ConvertToLocalizedFormula = tempCell.FormulaR1C1Local
        Else
            tempCell.formula = formulaToConvert
            ConvertToLocalizedFormula = tempCell.FormulaLocal
        End If
        tempCell.Clear
    End Function
    

    As an example, using this with the Hungarian locale in Immediate gives the following:

    Debug.Print ConvertToLocalizedFormula("=SUM($H5:$AE24)", xlA1, ActiveSheet.Range("AJ1"))
    =SZUM($H5:$AE24)
    
    Debug.Print ConvertToLocalizedFormula("=SUM(R5C7:R5C9)", xlR1C1, ActiveSheet.Range("AJ1"))
    =SZUM(S5O7:S5O9)
    

提交回复
热议问题