所以我的问题很简单。在我的VBA代码中,我从3个单元中检索3个值。 在下面的示例中,Value1 = 110,5; Value2 = 100; Value3 = 120
Value1 = Worksheets(Countryname).Cells(k, 5).Value
Value2 = Worksheets(Countryname).Cells(k, 14).Value
Value3 = Worksheets(Countryname).Cells(k, 23).Value
Cells(k, 4).Formula = "=MIN(" & Value1 & "," & Value2 & "," & Value3 & ")"
由于未知原因,Excel中显示的结果如下:
=MIN(110;50;100;130), instead of MIN(110,5;100;130)
问题出在以下事实:第一个变量转换为2个变量(110,5转换为110; 5)
您对此问题有解决方案吗?
在此先感谢您的帮助!
答案 0 :(得分:3)
您的逗号小数占位符与默认的EN-US列表分隔符冲突。使用.FormulaLocal并按照在工作表上显示的公式编写公式。
dim value1 as string, value2 as string, value3 as string
Value1 = Worksheets(Countryname).Cells(k, 5).text
Value2 = Worksheets(Countryname).Cells(k, 14).text
Value3 = Worksheets(Countryname).Cells(k, 23).text
Cells(k, 4).FormulaLocal = "=MIN(" & Value1 & ";" & Value2 & ";" & Value3 & ")"
'alternate with qualified cell addresses
Cells(k, 4).formula = "=min(" & Worksheets(Countryname).Cells(k, 5).address(external:=true) & "," & _
Worksheets(Countryname).Cells(k, 14).address(external:=true) & "," & _
Worksheets(Countryname).Cells(k, 23).address(external:=true) & ")"
看看您使用 k 的方式,可以很容易地推断出您正在运行类似for k=2 to lastRow
的循环。如果是这种情况,请立即编写所有公式。
with range(Cells(2, 4), cells(lastRow, 4))
.formula = "=min(" & Worksheets(Countryname).Cells(2, 5).address(0, 1, external:=true) & "," & _
Worksheets(Countryname).Cells(2, 14).address(0, 1, external:=true) & "," & _
Worksheets(Countryname).Cells(2, 23).address(0, 1, external:=true) & ")"
end with
如果要将值硬编码到工作表公式中,则最好将结果值写入其中。
Cells(k, 4) = application.min(Worksheets(Countryname).Cells(k, 5).value2, _
Worksheets(Countryname).Cells(k, 14).value2, _
Worksheets(Countryname).Cells(k, 23).value2)
答案 1 :(得分:1)
VBA可以以美国为中心。特别是由于公式用引号引起来,因此您应该使用系统列表分隔符,该分隔符似乎是;
而不是,
对于国际知名版本,请尝试:
sep = Application.International(xlListSeparator)
Cells(k, 4).FormulaLocal = "=MIN(" & Value1 & sep & Value2 & sep & Value3 & ")"