Excel宏,将在运行过程中的国际有效的公式(Excel Macro, inserting inte

2019-07-03 14:12发布

我有一个Excel电子表格,用宏,即插入一个条件格式,就像这样:

Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=UND($A3=""" & lastName & """; $B3=""" & firstName & """)"

正如你所看到的,我已经使用了德国的公式“和”(即“UND”),显然,这个代码不,只要我用它在法语或英语版本的Excel工作。 通常的公式被自动本地化,但我怎么可以插入在运行时一个公式,将在所有版本?

Answer 1:

好,感谢帮助我这个,你帮我破解这一个。

这的确是不可能只使用英语。 对公式进行操作时可以使用英语,例如。 通过设置编码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

返回的公式可以被使用FormatConditions ,这将被重新定位或未局部当原稿上的Excel不同语言版本以后打开。



Answer 2:

我只是找到了一个非常优雅的解决问题的方法在德国的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


将英文公式局部公式:

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

如果你需要处理的条件格式的公式,因为这些公式本地化公式总是存储(但你可能需要他们的通用版本,如使用,这是非常方便的Application.Evaluate(genericFormula)



Answer 3:

商店(的一个微不足道的版本)在您的工作簿(隐藏)单元格的公式。

然后,当您打开工作簿公式会自动用Excel为用户翻译。

现在你只需要在你的脚本来剖析这个公式(找到左括号“(”走的,过去的左侧:

使用这样的:

strLocalizedFormula = Mid(strYourFormula, 2, InStr(1, strYourFormula, "(") - 2)

其中strYourFormula会从你的工作表中公式的副本。

我希望这个工程,我只用英语环境。

此外,从阅读本: http://vantedbits.blogspot.nl/2010/10/excel-vba-tip-translate-formulas.html我想你应该(只)能够使用一个单元格公式的英文版本,从VBA。



Answer 4:

也许尝试这种(未经测试作为我只有英文版本insatlled)

写公式的你的国际版到了用路细胞的Range.Formula 。 然后阅读它回Range.FormulaLocal ,并写字符串到FormatConditions



Answer 5:

请参阅链接更多的解释: https://bettersolutions.com/csharp/excel-interop/locale-culture.htm

CultureInfo baseCulture = System.Threading.Thread.CurrentThread.CurrentCulture; 
Thread.CurrentThread.CurrentCulture = new CultureInfo(xlapp.LanguageSettings.LanguageID(Office.MsoAppLanguageID.msoLanguageIDUI)); 
// do something 
System.Threading.Thread.CurrentThread.CurrentCulture = baseCulture; 


Answer 6:

感谢大家! 我发现后非常有用的。

我的解决办法是其他的组合,我将其添加的情况下,有人发现它有用。

Dim tempform As String
Dim strlocalform1 As String
Dim strlocalform2 As String

' Get formula stored in WorksheetA Cell O1 =IFERROR(a,b)
tempform = Worksheets("Sheet").Range("O1").Formula

' Extract from the formula IFERROR statement in local language.
strlocalform1 = Mid(tempform, 2, InStr(1, tempform, "(") - 1)

' Extract from the formula separator , (comma) in local settings.
strlocalform2 = Mid(tempform, InStr(1, tempform, "a") + 1, 1)

' Add formula in local language to desired field.
pvt.CalculatedFields.Add Name:="NewField", Formula:="=" & strlocalform1 & "FORMULA" & strlocalform2 & ")"

希望这可以帮助!



文章来源: Excel Macro, inserting internationally valid formula during run-time