Page 2 of 2

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

Posted: Sun Jul 22, 2012 10:35 pm
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

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

Posted: Mon Jul 23, 2012 3:21 am
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 ดูนะคะ

ขอบคุณสำหรับคำแนะนำคะ

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

Posted: Mon Jul 23, 2012 5:47 pm
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

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

Posted: Mon Jul 23, 2012 8:12 pm
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 แต่หนูไม่รู้จะทำยังไงนะคะ

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

Posted: Mon Jul 23, 2012 8:19 pm
by snasui
:D แสดงผลเป็น Message ในลักษณะใดครับ ใน Code ที่เขียนมานั้น การแสดงเป็น Message Box ก็เขียนเป็นอยู่แล้วนี่ครับ

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

Posted: Mon Jul 23, 2012 9:07 pm
by hydrotaxonomy
คือในโค้ด ต้องไปเขียนค่าใน worksheets ก่อนนะคะ แล้วถึงเรียกตัวแปรนั้นมาแสดงบน msgBox นะคะ

หนูไม่ต้องการให้ แสดงค่าใน worksheets นะคะ แต่หนูไม่รู้ว่าต้องเขียนอย่างไร

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

Posted: Mon Jul 23, 2012 9:24 pm
by snasui
:D มีค่าใดบ้างที่ไม่ต้องเขียนให้ Worksheet ก่อน ช่วยอธิบายรายละเอียดเพิ่มเติมหรือจับภาพมาให้ดูด้วยครับ จาก Code ที่เขียนมาล่าสุดมีการบันทึกค่าลงไปใน Worksheet ด้วย ช่วยแนบไฟล์ตัวอย่างนั้นมาด้วยจะได้ทำความเข้าใจได้สะดวกครับ

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

Posted: Mon Jul 23, 2012 9:29 pm
by hydrotaxonomy
นี่คือค่าทั้งหมด ที่ต้องการให้แสดงคะ

มีเพียงค่า encode กับ decode เท่านั้นคะ ที่ต้องแสดงใน worksheets ก่อนแล้วจึงมาแสดงใน msgbox
ส่วนค่าเหลือมีตัวแปรเก็บไว้ดูได้นะคะ

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

Posted: Mon Jul 23, 2012 9:31 pm
by hydrotaxonomy
ไฟล์นี้คะ ขอบคุณคะ

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

Posted: Mon Jul 23, 2012 11:24 pm
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

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

Posted: Mon Jul 23, 2012 11:59 pm
by hydrotaxonomy
ขอบคุณคะ