Option Explicit
Public Function Diengiai(rngData As Range)
On Error Resume Next
Dim strText As String, strText2 As String
Dim i As Long, j As Long
Dim k
Dim subText() As String, dau() As String
strText = rngData.Formula
For i = 1 To Len(strText)
Select Case Mid(strText, i, 1)
Case "+", "-", "*", "/", "^", "(", ")"
ReDim Preserve dau(j)
dau(j) = Mid(strText, i, 1)
j = j + 1
End Select
Next i
strText = Trim$(Replace(strText, "=", ""))
strText = Replace(strText, "+", "@")
strText = Replace(strText, "-", "@")
strText = Replace(strText, "*", "@")
strText = Replace(strText, "/", "@")
strText = Replace(strText, "\", "@")
strText = Replace(strText, "^", "@")
strText = Replace(strText, "(", "@")
strText = Replace(strText, ")", "@")
subText = Split(strText, "@")
For i = 0 To UBound(subText)
If Not IsNumeric(subText(i)) Then
Err.Clear
k = Range(subText(i))
If Err.Number = 0 Then
If Len(Range(subText(i))) > 0 And Range(subText(i)).NumberFormat <> "General" Then
subText(i) = Format$(Range(subText(i)).Value, Range(subText(i)).NumberFormat)
ElseIf Len(Range(subText(i))) > 0 And Range(subText(i)).NumberFormat = "General" Then
subText(i) = Range(subText(i)).Value
End If
Else
subText(i) = subText(i)
End If
If Left(subText(i), 1) = "-" Then subText(i) = "(" & subText(i) & ")"
End If
Next i
ReDim Preserve dau(UBound(subText))
For i = 0 To UBound(subText)
strText2 = strText2 & subText(i) & dau(i)
Next i
Diengiai = strText2
End Function