:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser

อยากทราบวิธีตัดช่องว่างใน VBA

ฟอรัมถาม-ตอบปัญหาการใช้งาน Macro และ VBA
Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: อยากทราบวิธีตัดช่องว่างใน VBA

#21

Post by snasui »

:D จาก
snasui wrote: :D ลองยกตัวอย่างการเปรียบเทียบ Encode (IVTQSHI) กับ Longkey (POPPOPP) เหมือนการอธิบายในความเห็นก่อนหน้ามาให้ดูหน่อยครับว่ามันได้ผลลัพธ์ตัวแปร Decode เป็นคำว่า the best ได้อย่างไร
ลองเปรียบเทียบแบบจับคู่ให้กลับมาเป็น the best เหมือนกับอธิบายแบบจับคู่ตามด้านล่างให้ดูอีกทีครับ จากภาพที่แนบมาล่าสุดยังมองไม่ออกว่าจะเป็น the best ได้อย่างไร เดิมเคยเปรียบเทียบไว้ตามด้านล่างครับ
hydrotaxonomy wrote:หนูต้องการเอา Longkey P

เทียบกับ Plaintext t

ในที่นี้ จับคู่เทียบได้ Pt | Oh | Pe | Pb | Oe | Ps | Pt

จากตารางจะได้ IVTQSHI
hydrotaxonomy
Member
Member
Posts: 23
Joined: Tue Jul 17, 2012 11:35 pm

Re: อยากทราบวิธีตัดช่องว่างใน VBA

#22

Post by hydrotaxonomy »

1. หนูต้องการค่า decode กลับมาให้ได้ เหมือนค่า message ที่เราป้อนเข้าไปนะคะ

longkey = POPPOPP
Encode = IVTQSHI

ในที่นี้ จับคู่เทียบได้ PI | OV | PT | PQ| OS | PH | PI

จะได้ค่า decode = thebest
แล้วถ้าหนู ต้องการให้ ค่า decode = the best (เพิ่มช่องว่างเข้าไป) ต้องการให้ ค่า decode แสดงออกมาเหมือน ค่า message คะ


2. ถ้าหนูต้องการ ให้ ค่า encode มีช่องว่างเหมือนกับ message หนูต้อง mid อะไรคะ

โดย key = POP
message = the best
longkey = POPPOPP

ในที่นี้ จับคู่เทียบได้ Pt | Oh | Pe | _ช่องว่าง | Pb | Oe | Ps | Pt

จากตารางจะได้ IVT_QSHI คือจากเดิมใช้ PlainText เทียบ
แต่จะลองเปลี่ยนมาใช้ message เทียบกับ Longkey ดูนะคะ

ขอบคุณสำหรับคำแนะนำคะ
Attachments
vba7.jpg
vba7.jpg (87.28 KiB) Viewed 201 times
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: อยากทราบวิธีตัดช่องว่างใน VBA

#23

Post by snasui »

:D ตอบไปตามที่เข้าใจนะครับ คือต้องการให้ Decode มีจำนวนอักขระเท่า Encode วรรคให้เปลี่ยนเป็นเครื่องหมาย _ (Underscore)

ลองดูตัวอย่าง Code ตามด้านล่างครับ

Code: Select all

Public Sub Crypto01()
    Dim Key As String, PlainText As String, tempText As String
    Dim Longkey As String, Encode As String, Decode As String
    Dim t As String, i As Integer
    Key = InputBox("Enter the key of encryption")
    Key = UCase(Key)
    tempText = InputBox("Enter the message that you want to send")
    PlainText = Replace(tempText, " ", "")
    Longkey = Left(Application.Rept(Key, 10), Len(PlainText))
    Encode = ""
    For i = 1 To Len(Longkey)
        With Sheets("Sheet1").Application
            t = .Index(.Range("A2:Z27") _
                , .Match(Mid(PlainText, i, 1), .Range("A2:A27"), 0) _
                , .Match(Mid(Longkey, i, 1), .Range("A1:Z1"), 0))
        End With
        Encode = Encode & t
    Next i
    Decode = Application.Replace(Encode, InStr(tempText, " "), 0, "_")
    MsgBox Encode
    MsgBox Decode
End Sub
hydrotaxonomy
Member
Member
Posts: 23
Joined: Tue Jul 17, 2012 11:35 pm

Re: อยากทราบวิธีตัดช่องว่างใน VBA

#24

Post by hydrotaxonomy »

Code: Select all

Option Explicit
Public Sub Crypto()

Dim Key As String, Message As String, PlainText As String, Longkey As String, encode As String, _
      Decode As String, EnCol As String, EnRow As String
Dim i As Integer, p As Integer, r As Integer, c As Integer
    
Key = InputBox("Enter the key of encryption") 
Key = UCase(Key) 
Message = InputBox("Enter the message that you want to send")
Message = LCase(Message)
PlainText = Replace(Message, " ", "")
Longkey = Left(Application.WorksheetFunction.Rept(Key, 10), Len(PlainText))
MsgBox ("Key = " & Key) 
Worksheets("Sheet1").Range("B29").Value = Key
MsgBox ("Message = " & Message) ' show message
Worksheets("Sheet1").Range("B30").Value = Message 
MsgBox ("PlainText = " & PlainText) ' show Plaintext
Worksheets(Sheet1").Range("B31").Value = PlainText  
MsgBox ("Longkey = " & Longkey) ' show long key
Worksheets("Sheet1").Range("B32").Value = Longkey

p = 1
For i = 1 To Len(Message)   
        EnCol = Mid(Message, i, 1) 
            If EnCol = " " Then 
                encode = encode + " "
                p = p - 1
            Else
                EnRow = Mid(Longkey, p, 1)
                r = Application.Match(EnRow, Range("A2:A27"), 0)
                r = r + 1 
                c = Application.Match(EnCol, Range("A1:Z1"), 0) 
        encode = encode + Worksheets("Crypto").Cells(r, c).Value 
        End If
        p = p + 1
Next i
Worksheets("Crypto").Range("b33").Value = encode 
MsgBox ("Encode = " & encode) 

p = 1
For i = 1 To Len(Message) 
        EnCol = Mid(encode, i, 1)
            If EnCol = " " Then 
                Decode = Decode + " "
                p = p - 1
            Else
                EnRow = Mid(Longkey, p, 1) 
                r = Application.Match(EnRow, Range("A2:A27"), 0) 
                r = r + 1 'Not Found row A1
                c = Application.Match(EnCol, Range(Cells(r, 1), Cells(r, 26)), 0) 
        Decode = Decode + Worksheets("Crypto").Cells(1, c).Value  
        End If
        p = p + 1
Next i
Worksheets("Crypto").Range("b34").Value = Decode 
MsgBox ("Decode = " & Decode) 

MsgBox ("Formatting has been applied")
Range("B29:B34").ClearContents
Range("B29:B34").Style = "Normal"
MsgBox ("Original formatting")
End Sub

คือหนูอยากให้แสดงผล เป็นแบบ msgbox แต่หนูไม่รู้จะทำยังไงนะคะ
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: อยากทราบวิธีตัดช่องว่างใน VBA

#25

Post by snasui »

:D แสดงผลเป็น Message ในลักษณะใดครับ ใน Code ที่เขียนมานั้น การแสดงเป็น Message Box ก็เขียนเป็นอยู่แล้วนี่ครับ
hydrotaxonomy
Member
Member
Posts: 23
Joined: Tue Jul 17, 2012 11:35 pm

Re: อยากทราบวิธีตัดช่องว่างใน VBA

#26

Post by hydrotaxonomy »

คือในโค้ด ต้องไปเขียนค่าใน worksheets ก่อนนะคะ แล้วถึงเรียกตัวแปรนั้นมาแสดงบน msgBox นะคะ

หนูไม่ต้องการให้ แสดงค่าใน worksheets นะคะ แต่หนูไม่รู้ว่าต้องเขียนอย่างไร
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: อยากทราบวิธีตัดช่องว่างใน VBA

#27

Post by snasui »

:D มีค่าใดบ้างที่ไม่ต้องเขียนให้ Worksheet ก่อน ช่วยอธิบายรายละเอียดเพิ่มเติมหรือจับภาพมาให้ดูด้วยครับ จาก Code ที่เขียนมาล่าสุดมีการบันทึกค่าลงไปใน Worksheet ด้วย ช่วยแนบไฟล์ตัวอย่างนั้นมาด้วยจะได้ทำความเข้าใจได้สะดวกครับ
hydrotaxonomy
Member
Member
Posts: 23
Joined: Tue Jul 17, 2012 11:35 pm

Re: อยากทราบวิธีตัดช่องว่างใน VBA

#28

Post by hydrotaxonomy »

นี่คือค่าทั้งหมด ที่ต้องการให้แสดงคะ

มีเพียงค่า encode กับ decode เท่านั้นคะ ที่ต้องแสดงใน worksheets ก่อนแล้วจึงมาแสดงใน msgbox
ส่วนค่าเหลือมีตัวแปรเก็บไว้ดูได้นะคะ
Attachments
vba8.jpg
vba8.jpg (18.27 KiB) Viewed 187 times
Last edited by hydrotaxonomy on Mon Jul 23, 2012 9:33 pm, edited 1 time in total.
hydrotaxonomy
Member
Member
Posts: 23
Joined: Tue Jul 17, 2012 11:35 pm

Re: อยากทราบวิธีตัดช่องว่างใน VBA

#29

Post by hydrotaxonomy »

ไฟล์นี้คะ ขอบคุณคะ
Attachments
Test 1.xls
(45 KiB) Downloaded 18 times
User avatar
snasui
Site Admin
Site Admin
Posts: 31253
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: อยากทราบวิธีตัดช่องว่างใน VBA

#30

Post by snasui »

:D ลองลบค่าต่าง ๆ ในบรรทัดที่ 1:28 ในชีท Crypto ทิ้งไปแล้ว Run Code ด้านล่างดูครับ

Code: Select all

Public Sub Crypto()
    ''''''''''''''''''''''''''''' Declarations variable'''''''''''''''''''''''''''''''''''''''
    Dim Key As String, Message As String, PlainText As String, Longkey As String
    Dim encode As String, Decode As String, EnCol As String, EnRow As String
    Dim i As Integer, p As Integer, r As Integer, c As Integer
    Dim l As Integer, a(1 To 26, 1 To 26) As Variant, ta As Variant, tb(1 To 26) As Variant
    Dim m As Integer, t As String, n As Integer, o As Integer
    ta = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", _
        "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
    t = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    t = Application.Rept(t, 2)
    For l = 1 To 26
        For m = 1 To 26
            a(l, m) = Mid(t, m + n, 1)
        Next m
        n = n + 1
    Next l
    Key = InputBox("Enter the key of encryption") ' get Key
    Key = UCase(Key)  ' change all key to uppercase letters
    Message = InputBox("Enter the message that you want to send") ' get Message
    Message = LCase(Message) ' change all message to lowercase letter
    PlainText = Replace(Message, " ", "") ' cut space in Message by replacement and put into variable(Plaintext)
    Longkey = Left(Application.WorksheetFunction.Rept(Key, 10), Len(PlainText)) ' cut letter from left of Right_plaintext
    MsgBox ("Key = " & Key)  ' show key
    Worksheets("Crypto").Range("B29").Value = Key 'Set the value to the cell of the range"B29"
    MsgBox ("Message = " & Message) ' show message
    Worksheets("Crypto").Range("B30").Value = Message 'Set the value to the cell of the range"B30"
    MsgBox ("PlainText = " & PlainText) ' show Plaintext
    Worksheets("Crypto").Range("B31").Value = PlainText  'Set the value to the cell of the range"B30"
    MsgBox ("Longkey = " & Longkey) ' show long key
    Worksheets("Crypto").Range("B32").Value = Longkey 'Set the value to the cell of the range"B31"
    
    ''''''''''''''''''''''''''''''''' Encode ''''''''''''''''''''''''''''''''''''''''''''''
    p = 1
    For i = 1 To Len(Message)     ' Loop for Encode
        EnCol = Mid(Message, i, 1) ' Sub Message
        If EnCol = " " Then  ' Check if Sub Message equal space Add space to encode
            encode = encode + " "
            p = p - 1
        Else
            EnRow = Mid(Longkey, p, 1) ' Sub Longkey
            r = Application.Match(EnRow, ta, 0) 'find EnRow in column A return Index
            r = r + 1  'Not Found row A1
            c = Application.Match(EnCol, ta, 0) 'find EnCol in row And return Index
        encode = encode + a(r - 1, c) 'return the value to the cell
        End If
        p = p + 1
    Next i
    Worksheets("Crypto").Range("b33").Value = encode 'Set the value to the cell of the range"B33"
    MsgBox ("Encode = " & encode) ' show Encode
    '''''''''''''''''''''''''''''''''''''''''''Decode''''''''''''''''''''''''''''''''
    p = 1
    For i = 1 To Len(Message) ' Loop for Encode
        EnCol = Mid(encode, i, 1) ' Sub Encode
        If EnCol = " " Then ' Check if Sub Encol equal space Add space to encode
            Decode = Decode + " "
            p = p - 1
        Else
            EnRow = Mid(Longkey, p, 1) ' Sub Longkey
            r = Application.Match(EnRow, ta, 0) 'find EnRow in column A return Index
            r = r + 1 'Not Found row A1
            For o = 1 To 26
                tb(o) = a(r, o)
            Next o
            c = Application.Match(EnCol, tb, 0) + 1 'find Encol in row
            Decode = Decode + a(1, c)
        End If
        p = p + 1
    Next i
    Worksheets("Crypto").Range("b34").Value = LCase(Decode) 'Set the value to the cell of the range"B34"
    MsgBox ("Decode = " & Decode) ' show Decode
    'Restore the original style.("Normal" is a name for the default style.)
    MsgBox ("Formatting has been applied")
    Range("B29:B34").ClearContents
    Range("B29:B34").Style = "Normal"
    MsgBox ("Original formatting")
End Sub
hydrotaxonomy
Member
Member
Posts: 23
Joined: Tue Jul 17, 2012 11:35 pm

Re: อยากทราบวิธีตัดช่องว่างใน VBA

#31

Post by hydrotaxonomy »

ขอบคุณคะ
Post Reply