跳到主要內容

如何在Excel中生成一個隨機字符串?

有時您可能需要在單元格中生成隨機字符串,例如不同的密碼。 本文試圖向您展示一些技巧,以便在Excel中生成不同的隨機字符串。

使用公式生成隨機字符串
使用VBA代碼生成隨機字符串
使用Kutools for Excel輕鬆生成隨機字符串


使用公式生成隨機字符串

以下公式可以幫助您在Excel的範圍內生成隨機數,字母和字母數字值。

1。 要創建介於5和10000之間的99999位隨機數,請使用以下公式: = RANDBETWEEN(10000,99999),然後按 Enter 鍵,一個5位數字將顯示在一個單元格中,然後選擇該單元格並將填充手柄拖到要包含此公式的範圍內,已生成一系列5位數字,請參見屏幕截圖:

文檔生成字符串1 -2 文檔生成字符串2

備註:您可以更改參數以獲取需要。

2。 要創建隨機字母,請使用以下公式: = CHAR(RANDBETWEEN(65,90))。 此公式可以生成一個從a到z的隨機字母,如果需要創建四個隨機字母,則需要使用&字符添加字母。 如 = CHAR(RANDBETWEEN(65,90))&CHAR(RANDBETWEEN(65,90))&CHAR(RANDBETWEEN(65,90))&CHAR(RANDBETWEEN(65,90))。 然後,您將獲得四個隨機字母,然後將填充手柄拖到要包含此公式的範圍內。 看截圖:

文檔生成字符串3

筆記:

(1.)該公式在Excel 2007、2010和2013中有效,但在Excel 2003中無效。在Excel 2003中,請使用該公式 = CHAR(INT(RAND()* 25 + 65))&CHAR(INT(RAND()* 25 + 65))&CHAR(INT(RAND()* 25 + 65))和CHAR(INT(RAND()* * 25 + 65))

(2.)在公式65中為A,90為Z.

(3.)您可以使用&字符添加所需字母的數量。

3。 若要創建具有兩個字母和兩個數字的隨機字母數字字符串,請使用以下公式: = CHAR(RANDBETWEEN(65,90))&CHAR(RANDBETWEEN(65,90))&RANDBETWEEN(10,99) 並且您將在包含兩個字母和兩個數字的範圍內獲得以下字符串:

文檔生成字符串4

備註:您可以更改參數以獲取所需的數字,並使用&添加字母數。

輕鬆生成選定範圍內的隨機字符串:

生成帶有特定字符和文本長度的隨機字符串很容易, 插入隨機數據 的效用 Excel的Kutools.
立即下載 Kutools for Excel! (30 天免費試用)


使用VBA代碼生成隨機字符串

如果您覺得上述公式很困難且很麻煩,那麼以下VBA代碼可以幫助您輕鬆得多。 請這樣做:

1。 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications 窗口。

2。 點擊 插入 > 模塊,然後將以下宏粘貼到 模塊 窗口。

Public Function RandomizeF(Num1 As Integer, Num2 As Integer)
'Update 20131107
Dim Rand As String
Application.Volatile
getLen = Int((Num2 + 1 - Num1) * Rnd + Num1)
Do
    i = i + 1
    Randomize
    Rand = Rand & Chr(Int((85) * Rnd + 38))
Loop Until i = getLen
RandomizeF = Rand
End Function

3。 然後保存並關閉代碼,在單元格中輸入此功能 = RandomizeF(x,y) 插入隨機字符串,其最小長度為x個字符,最大長度為y個字符。

4。 在這個例子中,我將使用功能 = RandomizeF(5,10) 生成一個5到10個字符的字符串。 然後按 Enter 鍵,選擇單元格,然後將填充手柄拖到要包含此功能的範圍。 並隨機創建了5到10個字符的字母數字和特定字符串。 看截圖:

文檔生成字符串5


使用Kutools for Excel生成隨機字符串

有沒有一種方法可以生成帶有字母,數字和特殊字符或更多字符的隨機字符串? Excel的Kutools's 插入隨機數據 是一個出色的隨機數(和文本字符串)生成器,可以生成具有各種字符(包括字母,數字,特殊字符,空格甚至自定義字符串)的隨機數,隨機文本字符串或隨機數和文本符號。

申請前 Excel的Kutools首先下載並安裝.

1。 選擇一個將在其中生成隨機字符串的範圍,然後單擊 庫工具 > 插入 > 插入隨機數據。 見截圖:

3。 在 插入隨機數據 對話框,單擊 標籤,然後根據需要選擇字符類型,然後在 弦長 框,最後單擊 OK 按鈕。 看截圖:

然後,所選範圍已用隨機字符串填充。

尖端注意:如果要在範圍中生成或插入指定的格式數據字符串(例如???? @。??。com),則也可以使用此實用程序。 這樣做:

1。 選擇範圍並指定字符並檢查 面膜。 然後輸入所需的指定數據字符串。 看截圖:

備註:使用 ? 在最終指定的格式字符串中指示一位隨機字符。

2。 然後點擊 OK or 登記。 指定的格式數據字符串已按以下方式隨機生成。 看截圖:

Excel的Kutools's 插入隨機數據 可以幫助您輕鬆地在範圍單元格中處理以下操作。

  • 生成或插入範圍內的隨機數
  • 生成或插入範圍內的隨機日期
  • 生成或在範圍內插入自定義列表

  如果您想免費試用(30天)此實用程序, 請點擊下載,然後按照上述步驟進行操作。


演示:生成範圍內的隨機字符串


相關文章:

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!

Comments (29)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I found this useful as a starting point--I hadn't touched VBA in several years, so it was way easier to use the sample code here as a basis than start from scratch. Skelly1008, have you thought about something like this? Do Randomize charVal = (Int(85 * Rnd) + 48) If charVal > &H30 And charVal < &H7A Then If Not (charVal > &H5A And charVal < &H61) Then If Not (charVal > &H39 And charVal < &H41) Then newChar = Chr(charVal) Rand = Rand & newChar End If i = i + 1 End If End If Loop Until i = getLen That generates strings that contain only a-z, A-Z, and 0-9.
This comment was minimized by the moderator on the site
I'm looking for a code that can generate any number 0 to 9 and or any letter A to Z. But I need 25 characters in the that final out put.
This comment was minimized by the moderator on the site
Found way: 1st: For each char: =RANDBETWEEN(0;1) to randomly select a number or a letter (result in A2 to A9, for example) 2nd: =IF(An=0;RANDBETWEEN(0;9);CHAR(RAND()*26+97)) - in B2 to B9 -> to generate a number or a letter depending on result in A column 3rd: in the cell you want the generated password: =B2&B3&B4&B5&B6&B7&B8&B9
This comment was minimized by the moderator on the site
I live the formula but once I enter something in another field the numbers in the random fields change. I only want it to randomly generate one time. Not every time I enter data in other fields on the worksheet. Is that a separate function? Thanks!
This comment was minimized by the moderator on the site
Hi I want one help from you. I have to replace one last three values in this text for example: LoadTesting . I want to change only last three word of it
This comment was minimized by the moderator on the site
[quote]Hi I want one help from you. I have to replace one last three values in this text for example: LoadTesting . I want to change only last three word of itBy rOHIT[/quote] Assuming it is in cell A1:

=MID(A1,1,LEN(A1)-3)&"CAT"
This comment was minimized by the moderator on the site
Hi, On generating the random number in excel,can we insert the value to a form?any query plz share
This comment was minimized by the moderator on the site
This is several times better than I dared hope for!! I completely disregarded the idea of generating several values in a single cell. Guaranteed I'll be using this info every chance I get! 11/10.
This comment was minimized by the moderator on the site
Hi i would like to make the same in a MS WORD tab. is it possible ?
This comment was minimized by the moderator on the site
The VBA isn't working as a true random number. I'm using (10,12) as the criteria and if I run it down a couple thousand rows and do a countif formula in the adjacent column I find many duplicate passwords.
This comment was minimized by the moderator on the site
Very helpful, thank you. Using the VBA code, is it possible to only show letters and numbers and not other symbols?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations