我必须通过一个单元格的copy_paste值创建一个表。然后,这些值将与另一个单元格的值相乘。问题是“xlDecimalSeparator”。当它被设置为“。”时。没关系。使用“,”问题就开始了。 (我有荷兰设置)。我尝试做一个解决方法:将单元格声明为文本,然后将“,”替换为“。”,将值放入单元格,然后将单元格格式化为数字。 Everithing工作得很好,除了最后得到正确的值我必须双击单元格+输入得到结果公式否则我只是有这样的东西“= 3 * $ D $ 1”而不是6(结果功能)。
所以我的代码:
Sub Copy_Cell_test()
Dim my_range As Range
Dim strDecimal As String
Dim c As Variant
Application.ScreenUpdating = False
strDecimal = Application.International(xlDecimalSeparator)
If strDecimal = "," Then strDecimal = "."
Set my_range = Worksheets("sheet_test").Range("A2:H111")
With my_range
For Each c In my_range
If Not c.Value = "" Then
c.NumberFormat = "@"
c.Value = Replace(c.Value, ",", strDecimal)
c.Value = "=" & c.Value& & "*$D$1"
If Application.International(xlDecimalSeparator) = "," Then
c.Value = Replace(c.Value, strDecimal, ",")
End If
c.NumberFormat = "0.00"" €/lm"""
'c.Value = c.Value <<< Here I have error if "," is set
End If
Next c
End With
Application.ScreenUpdating = True
End Sub
我该如何解决?我尝试发送键F2 + [ENTER]但它不起作用。
答案 0 :(得分:0)
这对我有FormulaLocal
。
Option Explicit
Sub Copy_Cell_test()
Dim my_range As Range
Dim c As Range
Dim formulaText As String
On Error GoTo handleErr
Application.ScreenUpdating = False
Set my_range = Worksheets("sheet_test").Range("A2:H111")
With my_range
For Each c In my_range
If Not c.Value = "" Then
formulaText = "=" & c.Value & "*$D$1"
c.FormulaLocal = formulaText
c.NumberFormat = "0.00"" €/lm"""
End If
Next c
End With
Application.ScreenUpdating = True
handleErr:
If Err.Number = 0 Then Exit Sub
MsgBox Err.Description
End Sub