如何在Excel中生成或列出所有可能的排列?
例如,我有三個字符XYZ,現在,我想基於這三個字符列出所有可能的排列,以獲得六個不同的結果,例如:XYZ,XZY,YXZ,YZX,ZXY和ZYX。 在Excel中,如何根據不同的字符數快速生成或列出所有排列?
根據帶有VBA代碼的字符生成或列出所有可能的排列
以下VBA代碼可以幫助您根據字母的具體數量列出所有排列,請執行以下操作:
1。 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。
2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊 窗口。
VBA代碼:列出excel中所有可能的排列
Sub GetString()
'Updateby Extendoffice
Dim xStr As String
Dim FRow As Long
Dim xScreen As Boolean
xScreen = Application.ScreenUpdating
Application.ScreenUpdating = False
xStr = Application.InputBox("Enter text to permute:", "Kutools for Excel", , , , , , 2)
If Len(xStr) < 2 Then Exit Sub
If Len(xStr) >= 8 Then
MsgBox "Too many permutations!", vbInformation, "Kutools for Excel"
Exit Sub
Else
ActiveSheet.Columns(1).Clear
FRow = 1
Call GetPermutation("", xStr, FRow)
End If
Application.ScreenUpdating = xScreen
End Sub
Sub GetPermutation(Str1 As String, Str2 As String, ByRef xRow As Long)
Dim i As Integer, xLen As Integer
xLen = Len(Str2)
If xLen < 2 Then
Range("A" & xRow) = Str1 & Str2
xRow = xRow + 1
Else
For i = 1 To xLen
Call GetPermutation(Str1 + Mid(Str2, i, 1), Left(Str2, i - 1) + Right(Str2, xLen - i), xRow)
Next
End If
End Sub
3。 然後按 F5 鍵以運行此代碼,並彈出一個提示框,提醒您輸入要列出所有排列的字符,請參見屏幕截圖:
4。 輸入字符後,再單擊 OK 按鈕,所有可能的排列都顯示在活動工作表的A列中。 看截圖:
備註:如果輸入的字符長度等於或大於8個字符,則此代碼將無效,因為排列過多。
列出或生成多列中所有可能的組合
如果您需要基於多列數據生成所有可能的組合,也許沒有一種處理任務的好方法。 但, Excel的Kutools's 列出所有組合 實用程序可以幫助您快速輕鬆地列出所有可能的組合。 點擊下載Kutools for Excel!
Excel的Kutools:具有300多個方便的Excel加載項,可以在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%,每天為您減少數百次鼠標點擊!
Sort comments by
#39829
This comment was minimized by the moderator on the site
0
0
#39869
This comment was minimized by the moderator on the site
Report
0
0
#32450
This comment was minimized by the moderator on the site
Report
0
0
#29918
This comment was minimized by the moderator on the site
0
0
#29367
This comment was minimized by the moderator on the site
0
0
#29084
This comment was minimized by the moderator on the site
0
0
#29085
This comment was minimized by the moderator on the site
Report
0
0
#25087
This comment was minimized by the moderator on the site
0
0
#25048
This comment was minimized by the moderator on the site
0
0
#25049
This comment was minimized by the moderator on the site
0
0
#21598
This comment was minimized by the moderator on the site
Report
0
0
#21554
This comment was minimized by the moderator on the site
0
0
#18509
This comment was minimized by the moderator on the site
0
0
There are no comments posted here yet