Excel:组合多个(16)数据列以形成唯一组合

时间:2017-05-15 03:05:29

标签: excel vba combinations permutation

这是第一次,提前谢谢大家! 我的VBA经验很少,但相信解决方案可能需要它。

问题: 我有16列数据,每行8行:

A,C,E,G,I,K,M,O列:1,2,3,4,5,6,7,8

B,D,F,H,J,L,N,P:A,B,C,D,E,F,G,H

我的目标是使用16列中的每一列创建一列字符串,而不重复数字或字母。该字符串将保持数字 - 字母 - 数字 - 字母等命令。

例如:

接受的结果将是:A1B2C3D4E5F6G7H8,A1B2C3D4E5F6 G8H7

不包括重复的字母或数字: A 1B2C3D4E5F6G7 A 8,A 1 B 1 C3D4E5F6G7H8

这是可通过VBA编程的吗?非常感谢提前!

1 个答案:

答案 0 :(得分:0)

rows varable中你指定你有多少行,在变量cols你指定你有多少列(因为你告诉你有16列,我建议你把它保留为它是)。

Sub t()
Dim rows As Integer, cols As Integer
Dim str, strToAppend As String: str = ""

rows = 1
cols = 16

For i = 1 To rows
    For j = 2 To cols Step 2
        strToAppend = Cells(i, j).Value
        If InStr(str, strToAppend) = 0 Then
            str = str & strToAppend
        End If

        strToAppend = Cells(i, j - 1).Value
        If InStr(str, strToAppend) = 0 Then
            str = str & strToAppend
        End If
    Next j
    Cells(i, cols + 1).Value = str
    str = ""
Next i

End Sub