gro*_*vel 18 excel vba internationalization excel-vba excel-formula
我有一个带有宏的Excel电子表格,它插入条件格式,如下所示:
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=UND($A3=""" & lastName & """; $B3=""" & firstName & """)"
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我使用德语公式"AND"(即"UND"),显然,这个代码在法语或英语版本的Excel上使用时不会立即生效.通常公式是自动本地化的,但是如何在运行时插入一个适用于所有版本的公式?
gro*_*vel 14
好的,谢谢你帮我解决这个问题,你帮我解决了这个问题.
确实不可能只使用英语.在操作公式时可以使用英语,例如.通过设置编码Range("A1").formula="AND(TRUE)",但这不起作用FormatConditions.
我的解决方案是一个函数,它将一个公式临时写入一个单元格,通过该FormulaLocal属性读取它,并返回本地化的公式,如下所示:
Function GetLocalizedFormula(formula As String)
' returns the English formula from the parameter in the local format
Dim temporary As String
temporary = Range("A1").formula
Range("A1").formula = formula
Dim result As String
result = Range("A1").FormulaLocal
Range("A1").formula = temporary
GetLocalizedFormula = result
End Function
Run Code Online (Sandbox Code Playgroud)
可以使用返回的公式FormatConditions,稍后在不同语言版本的Excel上打开文档时,将重新本地化或取消本地化.
我刚刚在德语Excel论坛中找到了一个非常优雅的问题解决方案.这不会写入虚拟单元格,而是使用临时命名范围.我用原来的想法(信用到bst)为两个方向写了一个翻译函数.
将本地化公式转换为英文公式:
Public Function TranslateFormula_LocalToGeneric(ByVal iFormula As String) As String
Names.Add "temporaryFormula", RefersToLocal:=iFormula
TranslateFormula_LocalToGeneric = Names("temporaryFormula").RefersTo
Names("temporaryFormula").Delete
End Function
Run Code Online (Sandbox Code Playgroud)
将英语公式转换为本地化公式:
Public Function TranslateFormula_GenericToLocal(ByVal iFormula As String) As String
Names.Add "temporaryFormula", RefersTo:=iFormula
TranslateFormula_GenericToLocal = Names("temporaryFormula").RefersToLocal
Names("temporaryFormula").Delete
End Function
Run Code Online (Sandbox Code Playgroud)
如果您需要处理条件格式中的公式,这非常方便,因为这些公式总是存储为本地化公式(但您可能需要它们的通用版本,例如使用Application.Evaluate(genericFormula)).
在工作簿的(隐藏)单元格中存储(简单版本)公式。
然后,当您打开工作簿时,excel 将为用户自动翻译该公式。
现在你只需要在你的脚本中剖析这个公式(找到左括号“(”并从它的左边过去:
使用类似的东西:
strLocalizedFormula = Mid(strYourFormula, 2, InStr(1, strYourFormula, "(") - 2)
strYourFormula工作表中公式的副本在哪里。
我希望这有效,因为我只使用英语环境。
也从阅读这个:http : //vantedbits.blogspot.nl/2010/10/excel-vba-tip-translate-formulas.html 我想你应该(只)能够使用英文版的单元格公式来自VBA。
| 归档时间: |
|
| 查看次数: |
9082 次 |
| 最近记录: |