![]() ' This step is meant to scramble the characters in the string. OutputString = Join(toJoin, vbNullString) ToJoin(4) = GetRandomCharactersFromText(LETTERS_ALLOWED, countRemaining, duplicatesAllowed:=False) ' I arbitrarily pad the rest of the string with random letters, but you can change this logic. ToJoin(3) = GetRandomCharactersFromText(SYMBOLS_ALLOWED, minimumCountOfSymbols, duplicatesAllowed:=False) ToJoin(2) = GetRandomCharactersFromText(NUMBERS_ALLOWED, minimumCountOfNumbers, duplicatesAllowed:=False) ToJoin(1) = GetRandomCharactersFromText(LETTERS_ALLOWED, minimumCountOfLetters, duplicatesAllowed:=False) Private Function CreateRandomString(Optional ByVal lengthOfOutput As Long = 8, Optional ByVal minimumCountOfNumbers As Long = 1, Optional ByVal minimumCountOfLetters As Long = 1, Optional ByVal minimumCountOfSymbols As Long = 1) As StringĬountRemaining = lengthOfOutput - (minimumCountOfLetters + minimumCountOfNumbers + minimumCountOfSymbols)Ĭonst LETTERS_ALLOWED As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"Ĭonst NUMBERS_ALLOWED As String = "0123456789"Ĭonst SYMBOLS_ALLOWED As String = ' Change as necessary, I do not know what symbols you want included. With reference to the approach described in this answer ( ): Option Explicit If you enter 32768 (for example) or any larger number into the InputBox you should see an unhandled overflow error. Moreover, in this context where you're receiving arbitrary user input, Integer type overflows at 32768. My understanding is that you should use type Long instead of Integer under normal circumstances (as Integers are now converted to Longs under the hood).Your code may benefit from: L = InputBox("Amount of Passwords:", Type:=1) which, if I'm reading the documentation correctly, validates that the input is numeric.The length of the array could be controlled by the value returned by InputBox. In your case, you could create an array of random strings and then write the array to the sheet. Where possible, it's advisable to avoid using Activate and Select.RandomString = RandomString & StringArray(arrIndex, 2) ![]() StringArray(PositionCount, 2) = ThisWorkbook.Sheets("ChrSrc").Cells(Random(Lr), StringArray(PositionCount, 1))ĭo While Len(RandomString) < StringLength Select Case StringArray(PositionCount, 1) 'lastrow for each character category, adjust as needed StringArray(PositionCount, 1) = PositionCount StringArray(PositionCount, 1) = Random(4) ReDim StringArray(1 To StringLength, 1 To 3) Or add a loop and inputbox into this one. If you want to create multiple strings in one go, I would suggest writing a small caller sub, that calls this one x amount of times. After a character has been added, the zero in the array is changed to a one, to make sure every character gets used once.īy the end your random string is in the variable RandomString and you can write it to a cell or do whatever with it. Once the array has been filled, the do while loop is used to select the order of the characters randomly. These will be used to track which characters have been used in the random string. ![]() And lastly the third 'column' is filled with zeros. The second 'column' is then filled with the actual character to add to the random string. The other 4 (or more if the random string is longer) are randomly filled. The first four are always 1,2,3,4 to makes sure every type of character is used at least once. the first gives a number between 1 and 4 to determine from which column it should get it's character. From column A to D it's lower case letters, upper case letters, numbers, special characters.Īn array is made to store the characters and create the random string. The characters have been divided into four columns. Mine works like this: I have a sheet called ChrSrc with all characters that I want to be able to include in the string. I've adapted it to your constraints, but may have missed something. I use a different method but the result should be similar. I've tried a lot of googling to make it work specifically for this code but can't figure it out. This is the code I have but I can't figure out how to include these conditions to make it work as I need. My issue is that it populates only numbers and characters.ġ) I would like to include special characters to the mix and to make sure at least one special character is always included in each string.Ģ) I need to add a condition that each string always contains at least one upper case character, at least one lower case, at least one number. I have a code that generates random strings of characters (passwords) that contain exactly 8 characters.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |