Forum Discussion
Need code for ms excel
If I understand correctly, you want to create a VBA code that allows a user to enter a four-digit number, and the program will decode each digit of that number based on a predefined decryption key (2801 in this case). If a digit is not specified in the decryption key, it will remain unchanged.
Here's a VBA code that implements this functionality:
Vba Code is untested, please backup your file.
Sub DecryptNumber()
Dim decryptionKey As String
Dim inputNumber As String
Dim decryptedNumber As String
Dim i As Integer
' Define the decryption key
decryptionKey = "2801"
' Get the input number from the user
inputNumber = InputBox("Enter a four-digit number:")
' Check if the input number is valid
If Len(inputNumber) <> 4 Or Not IsNumeric(inputNumber) Then
MsgBox "Invalid input. Please enter a four-digit number."
Exit Sub
End If
' Decrypt each digit in the input number using the decryption key
decryptedNumber = ""
For i = 1 To Len(inputNumber)
If Mid(decryptionKey, i, 1) <> "" Then
decryptedNumber = decryptedNumber & Mid(decryptionKey, i, 1)
Else
decryptedNumber = decryptedNumber & Mid(inputNumber, i, 1)
End If
Next i
' Output the decrypted number
MsgBox "Decrypted number: " & decryptedNumber
End Sub
This code prompts the user to enter a four-digit number, decrypts each digit of that number based on the decryption key (2801), and then displays the decrypted number in a message box. If a digit is not specified in the decryption key, it remains unchanged.
- Prr1234Feb 28, 2024Copper ContributorCode is working fine. Thanks
But still there is some problem. That is it shows any 4 digit number to 2801 then combination of other 4 digit number should also automatically assigned accordingly as 2801. For example we have combination of 3 set of 4 digit number for decryption viz, first 8734 , second 3724, third 4425 and now we should assign each digit of the first number i.e. 8734 to 9501 and accordingly shows decrypted key for the remaining set of numbers.- Prr1234Feb 28, 2024Copper Contributorvb code to make program to take any digit and assign it as 9, 5, 0 and 1 and stored it and then again take another group of four digit number and assign each digit accordingly as per 9,5,0,1 if found or leave a (.) In that place but the first group of four digit number should be assigned to 9501 then take another four digit number and if the digit repeats it shows 9 or 5 or 0 or 1 or leave a (.) in that place so that a set of decrypted keys could be traced
- NikolinoDEFeb 28, 2024Platinum Contributor
Dim decryptionKey As String Function EncryptNumber(inputNumber As String) As String Dim encryptedNumber As String Dim i As Integer decryptionKey = "9501" ' Set the initial decryption key For i = 1 To Len(inputNumber) encryptedNumber = encryptedNumber & GetEncryptedDigit(Mid(inputNumber, i, 1)) Next i EncryptNumber = encryptedNumber End Function Function GetEncryptedDigit(digit As String) As String If InStr(decryptionKey, digit) > 0 Then GetEncryptedDigit = Mid(decryptionKey, InStr(decryptionKey, digit), 1) Else GetEncryptedDigit = "." End If End Function Function DecryptNumber(encryptedNumber As String) As String Dim decryptedNumber As String Dim i As Integer For i = 1 To Len(encryptedNumber) decryptedNumber = decryptedNumber & GetDecryptedDigit(Mid(encryptedNumber, i, 1)) Next i DecryptNumber = decryptedNumber End Function Function GetDecryptedDigit(encryptedDigit As String) As String Dim index As Integer index = InStr(decryptionKey, encryptedDigit) If index > 0 Then GetDecryptedDigit = Mid("1234567890.", index, 1) Else GetDecryptedDigit = "." End If End FunctionTry this code, maybe it will help you.
My understanding is no more than that when it comes to translating the project and the project itself
.