กระทู้เก่าบอร์ด อ.Yeadram
        
           2,788   14		  
          
					  
		    URL.หัวข้อ / 
		    URL
        
        ขอ Function แทรก "," ทุก 3 ตัวอักษรครับ      
    
      ตัวอย่าง
ABCDEFG
cf
ghpo
iirt
G5HJKL
Output ที่ต้องการ
A,BCD,EFG
cf
g,hpo
i,irt
G5H,JKL
รบกวนด้วยครับ ขอบคุณครับ
    
  ABCDEFG
cf
ghpo
iirt
G5HJKL
Output ที่ต้องการ
A,BCD,EFG
cf
g,hpo
i,irt
G5H,JKL
รบกวนด้วยครับ ขอบคุณครับ
				14 Reply in this Topic. Dispaly 1  pages and you are on page number 1 
				
        
    2 @R08014    
        
  
      ไม่เป็นไร ได้แล้วครับ
Function InsertComma(x) As String
If Len(x) < 4 Then
InsertComma = x
End If
If Len(x) > 3 And Len(x) < 7 Then
InsertComma = Left(x, Len(x) - 3) & "," & Right(x, 3)
End If
If Len(x) > 6 And Len(x) < 10 Then
InsertComma = Left(x, Len(x) - 6) & "," & Left(Right(x, 6), 3) & "," & Right(x, 3)
End If
End Function
    
    
  Function InsertComma(x) As String
If Len(x) < 4 Then
InsertComma = x
End If
If Len(x) > 3 And Len(x) < 7 Then
InsertComma = Left(x, Len(x) - 3) & "," & Right(x, 3)
End If
If Len(x) > 6 And Len(x) < 10 Then
InsertComma = Left(x, Len(x) - 6) & "," & Left(Right(x, 6), 3) & "," & Right(x, 3)
End If
End Function
        
    3 @R08015    
        
  
      Function InsertStr(Exp As String, Ins As String, Every As Integer)
Dim Sz As Integer, Nw As String
Sz = Len(Exp)
For i = 1 To Sz Step Every
Nw = Nw & Ins & Mid(Exp, i, Every)
Next
InsertStr = Mid(Nw, 2)
End Function
ผมลองทำสูตรมาให้นะครับ
หวังว่าคงจะใช้เป็น
    
  Dim Sz As Integer, Nw As String
Sz = Len(Exp)
For i = 1 To Sz Step Every
Nw = Nw & Ins & Mid(Exp, i, Every)
Next
InsertStr = Mid(Nw, 2)
End Function
ผมลองทำสูตรมาให้นะครับ
หวังว่าคงจะใช้เป็น
        
    4 @R08016    
        
  
      เอ้าได้แล้วหรอ... ช้าไป 2 นาที...
มะเป็นไรครับ สูตรที่ผมเขียนเก็บไว้ใช้ได้ ผมก็กะเก็บไว้เหมือนกัน น่าจะมีประโยชน์ในภายหลัง
แต่ว่ารู้สึกลอจิกของคุณ krathok-man จะซับซ้อนกว่าหน่อย
ว่าแล้วเชียว...เลยถามย้ำไปว่าถูกหรือเปล่า ลอจิกก็คือให้ตัดมาจากข้างหลังก่อนใช่ไหมครับ
    
  มะเป็นไรครับ สูตรที่ผมเขียนเก็บไว้ใช้ได้ ผมก็กะเก็บไว้เหมือนกัน น่าจะมีประโยชน์ในภายหลัง
แต่ว่ารู้สึกลอจิกของคุณ krathok-man จะซับซ้อนกว่าหน่อย
ว่าแล้วเชียว...เลยถามย้ำไปว่าถูกหรือเปล่า ลอจิกก็คือให้ตัดมาจากข้างหลังก่อนใช่ไหมครับ
        
    5 @R08017    
        
  
      จัดให้ครับ เพิ่มโค๊ดอีกหน่อย....
Function InsertStr(Exp As String, Ins As String, Every As Integer, Optional Rev As Boolean = False)
Dim Sz As Integer, Nw As String
Dim St As Integer, En As Integer, Stp As Integer
Sz = Len(Exp)
If Rev Then
St = Sz
En = 1
Stp = -Every
Else
St = 1
En = Sz
Stp = Every
End If
For i = St To En Step Stp
If Rev Then
Nw = Ins & Mid(Exp, i, Every) & Nw
Else
Nw = Nw & Ins & Mid(Exp, i, Every)
End If
Next
InsertStr = Mid(Nw, 2)
End Function
    
    
  Function InsertStr(Exp As String, Ins As String, Every As Integer, Optional Rev As Boolean = False)
Dim Sz As Integer, Nw As String
Dim St As Integer, En As Integer, Stp As Integer
Sz = Len(Exp)
If Rev Then
St = Sz
En = 1
Stp = -Every
Else
St = 1
En = Sz
Stp = Every
End If
For i = St To En Step Stp
If Rev Then
Nw = Ins & Mid(Exp, i, Every) & Nw
Else
Nw = Nw & Ins & Mid(Exp, i, Every)
End If
Next
InsertStr = Mid(Nw, 2)
End Function
        
    6 @R08022    
        
  
      บอกหน่อยได้มั้นครับว่า เอาไปใช้ประโยชน์ยังงัย นึกไม่ออก    
    
  
        
    7 @R08027    
        
  
      ไม่ต้องนึกหรอกครับ 
 
คุณ Krathok-man ช่วยตอบหน่อยสิครับ
    
    
   
 
คุณ Krathok-man ช่วยตอบหน่อยสิครับ
        
    8 @R08031    
        
  
      ผมต้องการเปลี่ยนเลข อารบิคเป็นเลขไทย ครับ เช่น 1 เป็น ๑ 
แต่เปลี่ยนเฉยๆ ไม่พอ ต้องการให้วางเครื่องหมาย "," คั่น เหมือน Stardard
Format ด้วย เช่น ๑๒๔,๕๖๖.oo เป็นต้น
    
  แต่เปลี่ยนเฉยๆ ไม่พอ ต้องการให้วางเครื่องหมาย "," คั่น เหมือน Stardard
Format ด้วย เช่น ๑๒๔,๕๖๖.oo เป็นต้น
        
    9 @R08033    
        
  
      แล้วทำไงอะครับ ขอสูตรเต็มได้เปล่า    
    
  
        
    10 @R08036    
        
  
      จริงสูตรที่ผมให้ไปแบบเช็คย้อนกลับ(ตอบกระทู้ลำดับที่ 5) นั้นไม่ถูกต้องนะครับ
ขอแก้เป็น
----------------------------------------------------------------------------------
Function InsertStr(Exp As String, Ins As String, Every As Integer, Optional Rev As Boolean = False)
Dim Sz As Integer, Nw As String, St As Integer, Fc As String
Sz = Len(Exp)
St = (Sz Mod Every) * Abs(Rev)
Nw = Mid(Exp, 1, St)
For i = 1 + St To Sz Step Every
Nw = Nw & Ins & Mid(Exp, i, Every)
Next
InsertStr = Mid(Nw, 2)
If St Then InsertStr = Nw
End Function
----------------------------------------------------------------------------------
จะสามารถ insert string แบบข้างหน้าหรือข้างหลังได้ด้วย
ส่วนสูตรจัด format ตัวเลขให้เป็นเลขไทยผมลองเขียนเล่นๆดู
Function ThaiNumber(Exp As Double, Optional strFormat As String = "0.00")
Dim Nw As String
Nw = Format(Exp, strFormat)
For i = 0 To 9
Nw = Replace(Nw, i, Chr(i + 240))
Next
ThaiNumber = Nw
End Function
    
    
  ขอแก้เป็น
----------------------------------------------------------------------------------
Function InsertStr(Exp As String, Ins As String, Every As Integer, Optional Rev As Boolean = False)
Dim Sz As Integer, Nw As String, St As Integer, Fc As String
Sz = Len(Exp)
St = (Sz Mod Every) * Abs(Rev)
Nw = Mid(Exp, 1, St)
For i = 1 + St To Sz Step Every
Nw = Nw & Ins & Mid(Exp, i, Every)
Next
InsertStr = Mid(Nw, 2)
If St Then InsertStr = Nw
End Function
----------------------------------------------------------------------------------
จะสามารถ insert string แบบข้างหน้าหรือข้างหลังได้ด้วย
ส่วนสูตรจัด format ตัวเลขให้เป็นเลขไทยผมลองเขียนเล่นๆดู
Function ThaiNumber(Exp As Double, Optional strFormat As String = "0.00")
Dim Nw As String
Nw = Format(Exp, strFormat)
For i = 0 To 9
Nw = Replace(Nw, i, Chr(i + 240))
Next
ThaiNumber = Nw
End Function
        
    11 @R08040    
        
  
      Code เต็มเป็นดังนี้ พอดียังไม่ได้เกา นะครับ ตัวแปรบางตัวก็เลยตั้งชื่อแปลกๆ หน่อย
แต่ลองดูใช้ได้แล้วครับ Code บางส่วน(ตรงกลาง) ลอกมาจากคุณ YEADRAM หากต้องการกลับไปดูที่มาที่ไป ให้ Searh ใน WEBROD ว่า "เลขไทย" ครับ
Function ThNum(Text0)
If Len(Text0) < 1 Then
ThNum = vbNull
Else
Text0 = Round(Text0, 2)
Dim i As Integer
Dim sq
sq = ""
For i = 1 To Len(Text0)
If Asc(Mid(Text0, i, 1)) >= 48 And Asc(Mid(Text0, i, 1)) <= 57 Then
sq = sq & Chr(Asc(Mid(Text0, i, 1)) + 192)
Else
sq = sq & Mid(Text0, i, 1)
End If
Next
Dim A
Dim B
i = InStr(sq, ".")
A = Left(sq, i - 1)
If Len(A) < 1 Then
A = Chr(240)
Else
A = InsertComma(A)
End If
B = Right(sq, 3)
ThNum = A & B
End If
End Function
Function InsertComma(x) As String
If Len(x) < 4 Then
InsertComma = x
End If
If Len(x) > 3 And Len(x) < 7 Then
InsertComma = Left(x, Len(x) - 3) & "," & Right(x, 3)
End If
If Len(x) > 6 And Len(x) < 10 Then
InsertComma = Left(x, Len(x) - 6) & "," & Left(Right(x, 6), 3) & "," & Right(x, 3)
End If
End Function
    
    
  แต่ลองดูใช้ได้แล้วครับ Code บางส่วน(ตรงกลาง) ลอกมาจากคุณ YEADRAM หากต้องการกลับไปดูที่มาที่ไป ให้ Searh ใน WEBROD ว่า "เลขไทย" ครับ
Function ThNum(Text0)
If Len(Text0) < 1 Then
ThNum = vbNull
Else
Text0 = Round(Text0, 2)
Dim i As Integer
Dim sq
sq = ""
For i = 1 To Len(Text0)
If Asc(Mid(Text0, i, 1)) >= 48 And Asc(Mid(Text0, i, 1)) <= 57 Then
sq = sq & Chr(Asc(Mid(Text0, i, 1)) + 192)
Else
sq = sq & Mid(Text0, i, 1)
End If
Next
Dim A
Dim B
i = InStr(sq, ".")
A = Left(sq, i - 1)
If Len(A) < 1 Then
A = Chr(240)
Else
A = InsertComma(A)
End If
B = Right(sq, 3)
ThNum = A & B
End If
End Function
Function InsertComma(x) As String
If Len(x) < 4 Then
InsertComma = x
End If
If Len(x) > 3 And Len(x) < 7 Then
InsertComma = Left(x, Len(x) - 3) & "," & Right(x, 3)
End If
If Len(x) > 6 And Len(x) < 10 Then
InsertComma = Left(x, Len(x) - 6) & "," & Left(Right(x, 6), 3) & "," & Right(x, 3)
End If
End Function
        
    12 @R08042    
        
  
      อืมห์ code ยาวเชียว...
ได้ลอง code ที่ผมให้ไปปะครับ
    
  ได้ลอง code ที่ผมให้ไปปะครับ
        
    13 @R08044    
        
  
      ผมนึกสนุกนิดหน่อย เลยจับโค้ดของคุณ Kratok-man มาขัดมาดัดให้มันสั้น เล่นๆ นะครับ
    
  Function ThNum(dblMoney as double)
If Len(dblMoney) < 1 Then
ThNum = vbNull 
Exit function
end if
Dim i As Integer
Dim tx0
Dim sq
tx0 = Format(Round(dblMoney, 2), "##,###,###.00")
sq = ""
For i = 1 To Len(tx0)
If Asc(Mid(tx0, i, 1)) >= 48 And Asc(Mid(tx0, i, 1)) <= 57 Then
sq = sq & Chr(Asc(Mid(tx0, i, 1)) + 192)
Else
sq = sq & Mid(tx0, i, 1)
End If
Next
ThNum = sq
End Function    
        
    14 @R08045    
        
    
      เปลี่ยนเป็นเลขไทย ผมก็เคยเอาโคตในห้องนี้ไปแปลง ครับ น่าจะมีอยู่แล้ว    
    
  
      Time: 0.4877s
    
      
		
แล้ว output ที่ให้มาผิดหรือเปล่าครับ
A,BCD,EFG
จะให้มันตัดทุกๆสามอย่างไรครับ บอกลอจิกหน่อย