Skip to content

Instantly share code, notes, and snippets.

@touchiep
Last active December 12, 2023 10:46
Show Gist options
  • Save touchiep/daeefacbc904c88bde44a1e6c9b300b0 to your computer and use it in GitHub Desktop.
Save touchiep/daeefacbc904c88bde44a1e6c9b300b0 to your computer and use it in GitHub Desktop.
แปลงเลขอารบิกเป็นไทย หรือ แปลงเลขไทยเป็นอารบิก สำหรับ Microsoft Excel
Sub W2THConvert()
'Numeral converter 1.0
'copyright 2022 Pongsathorn Sraouthai
'for convert western numeral to Thai numeral or Thai numeral to western numeral.
Dim r As Range
Dim c As Range
Dim cdata
Dim copt
Dim cdp
Dim LangID, LangUI(6)
LangID = Application.International(xlCountryCode)
Select Case LangID
Case 66
LangUI(1) = "โปรดคลิกเลือกเซลล์ที่ต้องการแปลง (เลือกหลายเซลล์ได้)"
LangUI(2) = "เลือกเซลล์"
LangUI(3) = "เลือกวิธีการแปลงตัวเลข"
LangUI(4) = "1 แปลงเลขอารบิกเป็นเลขไทย"
LangUI(5) = "2 แปลงเลขไทยเป็นอารบิก"
LangUI(6) = "การแปลงตัวเลข"
Case Else
LangUI(1) = "Please click to select a cell or multiple cells"
LangUI(2) = "Select cells"
LangUI(3) = "Please select type of converter"
LangUI(4) = "1 Convert western numeral to Thai numeral"
LangUI(5) = "2 Convert Thai numeral to Western numeral"
LangUI(6) = "Number converter"
End Select
On Error GoTo exitsub
Set r = Application.Selection.Range("A1")
Set r = Application.InputBox(LangUI(1), LangUI(2), r.Address, , , , , 8)
If r.Address = "" Then Exit Sub
copt = InputBox(LangUI(3) & vbCrLf & LangUI(4) & vbCrLf & LangUI(5), LangUI(6), 1)
If copt = "" Then Exit Sub
r.Select
For Each c In r.Cells
cdata = c.Value
Select Case copt
Case "1"
If IsNumeric(cdata) Then
cdp = InStr(1, GetCellFormat(c), "General", vbTextCompare)
If cdp > 0 Then
cdp = GetDPstr(CStr(cdata))
If cdp <> 0 Then
c.NumberFormat = "t0." & String(cdp, "0")
Else
c.NumberFormat = "t0"
End If
ElseIf InStr(1, GetCellFormat(c), "$-1") > 0 Then
cdp = Replace(GetCellFormat(c), "$-1", "$-D")
c.NumberFormat = cdp
Else
c.NumberFormat = "t" & GetCellFormat(c)
End If
Else
cdata = W2TH(CStr(cdata))
c.Value = cdata
End If
Case "2"
If IsNumeric(cdata) Then
cdp = InStr(1, GetCellFormat(c), "t")
If cdp <> 0 Then
c.NumberFormat = Mid(GetCellFormat(c), 2)
ElseIf InStr(1, GetCellFormat(c), "$-D") Then
cdp = Replace(GetCellFormat(c), "$-D", "$-1")
c.NumberFormat = cdp
Else
c.NumberFormat = "General"
End If
Else
cdata = TH2W(CStr(cdata))
c.Value = cdata
End If
Case Else
Exit Sub
End Select
Next c
exitsub:
Exit Sub
End Sub
Function TH2W(strInput As String) As String
'Convert Thai numeral to Western numeral
Dim numberArray
numberArray = Array(ChrW(3664), "0", _
ChrW(3665), "1", _
ChrW(3666), "2", _
ChrW(3667), "3", _
ChrW(3668), "4", _
ChrW(3669), "5", _
ChrW(3670), "6", _
ChrW(3671), "7", _
ChrW(3672), "8", _
ChrW(3673), "9")
Dim i As Long
TH2W = strInput
For i = 0 To 18 Step 2
TH2W = Replace(TH2W, numberArray(i), numberArray(i + 1))
Next i
End Function
Function W2TH(strInput As String) As String
'Convert Western numeral to Thai numeral
Dim numberArray
numberArray = Array("0", ChrW(3664), _
"1", ChrW(3665), _
"2", ChrW(3666), _
"3", ChrW(3667), _
"4", ChrW(3668), _
"5", ChrW(3669), _
"6", ChrW(3670), _
"7", ChrW(3671), _
"8", ChrW(3672), _
"9", ChrW(3673))
Dim i As Long
W2TH = strInput
For i = 0 To 18 Step 2
W2TH = Replace(W2TH, numberArray(i), numberArray(i + 1))
Next i
End Function
Private Function GetCellFormat(cell As Range)
GetCellFormat = cell.NumberFormatLocal
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment