Forum Discussion
Prr1234
Feb 27, 2024Copper Contributor
Need code for ms excel
I need help for the following. Lets see an examples Suppose we have digits 0 to 9 only. If we are assigning any four digit number like 7856 as 2801 ( In this case we always alott 2801 to any desired...
Prr1234
Feb 27, 2024Copper Contributor
Thanks for the reply but my concern is that the decrypted key is 2801 and any 4 digit number entered by the user will be assigned to 2801 as decrypted key and as per that remaining number from 0 to 9 will be decoded if available or leave blank space si that later a strip of decrypted keys could be find manually
NikolinoDE
Feb 28, 2024Platinum Contributor
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.