Excel VBA use localized RC formula for Conditional Formatting?

后端 未结 2 1653
半阙折子戏
半阙折子戏 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:19

    Language independent Excel formulae for use in conditional formatting or validation rules

    Write your formula with english function names, with either A1- or R1C1-references. Assign this formula as the cell formula to an appropriate cell. This could be the cell you are about to format, or any unused cell. Then read the FormulaLocal from that cell - this is the formula you are going to use for formatting. Set the formula of the cell you used as translator to blank.

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