Các bác giúp em với! file và code của em (sưu tầm tren diễn đàn GPE) đây:
Function FormatWithComma(ByVal number As Double) As String
Dim text As String, result As String
text = Format(2001 / 2, "#,##0.0")
result = Format(number, "#,##0.0##")
If Mid(text, 6, 1) = "," Then
FormatWithComma = Replace(result, Mid(text, 2, 1), ".")
Else
result = Replace(result, ".", "@")
result = Replace(result, Mid(text, 2, 1), ".")
FormatWithComma = Replace(result, "@", ",")
End If
End Function
Private Sub Worksheet_Change(ByVal Target As Range)
Dim expression As String, result As Double
If Not Intersect(Target, Range("D1

10000")) Is Nothing Then
If Target.Count = 1 Then
If Target.Value <> "" Then
If InStr(Target, ":") > 0 Then
expression = Trim(Split(Target.Value, ":")(1))
Else
expression = Trim(Target.Value)
End If
expression = Replace(expression, ",", ".")
Application.EnableEvents = False
On Error GoTo end_
result = Evaluate(expression)
Target.Offset(, 2).Value = result
Target.Value = Trim(Target.Value) & " = " & FormatWithComma(result)
End If
End If
End If
end_:
Application.EnableEvents = True
End Sub
_______________________________________________________
Code trên còn tồn tại nhược điểm sau:
Khi đả đánh vào:
Móng M1: 1,2*2*3 -> enter ra: 1,2*2*3 = 7,2. Trong quá trình tính toán em edit thì:
- Nếu sửa: Móng M1: 1,1*2*3 (sửa "1,2"->1,1 và xóa "=7,2") thi ra kết quả đúng.
- Nếu sửa: Móng M1:1,1*2*3 = 7,2 thi hiển thị kết quả: Móng M1: 1,1*2*3 = 7,2 = 0,00 (sai) em muốn chi edit biểu thức thì nó tự replece kết quả được không?.
Chúc các bác sức khỏe!