Excel 组合发电机仍保持正常

Excel 组合发电机仍保持正常,excel,vba,Excel,Vba,不知是否有人能帮我。我被难住了。我已经很久没有用excel了 我有9列,每个单元格中有不同的值,每列的单元格数不同 我需要一个公式/宏来吐出所有单元格的组合,但仍然保持列的完全相同顺序 比如说 栏目: D / 003 / 23 / 3 / 3R / C / VFX ... / 005 / 48 / 3 / 12 / .. / VDF ... / 007 / ... / 1 / ... /... / HSF 它像这样吐出来: Option Base 1 Option Expli

不知是否有人能帮我。我被难住了。我已经很久没有用excel了

我有9列,每个单元格中有不同的值,每列的单元格数不同

我需要一个公式/宏来吐出所有单元格的组合,但仍然保持列的完全相同顺序

比如说 栏目:

D   / 003 / 23  / 3 / 3R  / C  / VFX

... / 005 / 48  / 3 / 12  / .. / VDF

... / 007 / ... / 1 / ... /... / HSF
它像这样吐出来:

Option Base 1
Option Explicit

Function combinationNo(r As Range, serialNumber As Integer)
' find the number of entries in each column in range r
' and pick the Nth combination - where serialNumber = 0 
' gives the top row
' assumes not all columns are same length
' but are filled starting with the first row

Dim ePerRow()
Dim columnIndex As Integer
Dim totalElements As Integer
Dim i, col
Dim tempString As String

ReDim ePerRow(r.Columns.Count)
totalElements = 1
i = 0
For Each col In r.Columns
  i = i + 1
  ePerRow(i) = Application.WorksheetFunction.CountA(col)
 totalElements = totalElements * ePerRow(i)
Next

If serialNumber >= totalElements Then
  combinationNo = "Serial number too large"
  Exit Function
End If

tempString = ""
For i = 1 To UBound(ePerRow)
  totalElements = totalElements / ePerRow(i)
  columnIndex = Int(serialNumber / totalElements)
  tempString = tempString & r.Cells(columnIndex + 1, i).Value
  serialNumber = serialNumber - columnIndex * totalElements
Next i

combinationNo = tempString

End Function
D0032333RCVFX

D0032333RCVDF

D0032333RCHSF

D0034833RCVFX

D0034833RCVDF

等等。。。。
以此类推……

您可能希望使用“序列号”调用此函数,以便可以调用“第n个组合”。然后问题分为两部分:

第1部分:计算出,对于给定的“序列号”,您需要每个列的哪个元素。如果你在每列中有相同数量的元素E,这将很简单:就像在E的底端写N一样。当每列中的元素数量不同时,这就有点棘手了——类似这样:

Option Base 1
Option Explicit

Function combinationNo(r As Range, serialNumber As Integer)
' find the number of entries in each column in range r
' and pick the Nth combination - where serialNumber = 0 
' gives the top row
' assumes not all columns are same length
' but are filled starting with the first row

Dim ePerRow()
Dim columnIndex As Integer
Dim totalElements As Integer
Dim i, col
Dim tempString As String

ReDim ePerRow(r.Columns.Count)
totalElements = 1
i = 0
For Each col In r.Columns
  i = i + 1
  ePerRow(i) = Application.WorksheetFunction.CountA(col)
 totalElements = totalElements * ePerRow(i)
Next

If serialNumber >= totalElements Then
  combinationNo = "Serial number too large"
  Exit Function
End If

tempString = ""
For i = 1 To UBound(ePerRow)
  totalElements = totalElements / ePerRow(i)
  columnIndex = Int(serialNumber / totalElements)
  tempString = tempString & r.Cells(columnIndex + 1, i).Value
  serialNumber = serialNumber - columnIndex * totalElements
Next i

combinationNo = tempString

End Function
使用列所在的范围和序列号(从0开始表示“仅限顶行”)调用此函数。它假定每列的底部都有空格。否则,它将返回一个字符串,该字符串是每列中值组合的串联,正如您所描述的

编辑下面的图片可能会有所帮助,它显示了如何使用该工具以及它的实际用途。请注意,第一个引用(指向不同长度的列的表)是绝对引用(使用
$
符号,因此当您将其从一个单元格复制到另一个单元格时,它始终引用相同的范围),而第二个参数是相对的(因此它依次指向
0、1、2、3
等)


您可能希望使用“序列号”调用此函数,以便调用“第n个组合”。然后问题分为两部分:

第1部分:计算出,对于给定的“序列号”,您需要每个列的哪个元素。如果你在每列中有相同数量的元素E,这将很简单:就像在E的底端写N一样。当每列中的元素数量不同时,这就有点棘手了——类似这样:

Option Base 1
Option Explicit

Function combinationNo(r As Range, serialNumber As Integer)
' find the number of entries in each column in range r
' and pick the Nth combination - where serialNumber = 0 
' gives the top row
' assumes not all columns are same length
' but are filled starting with the first row

Dim ePerRow()
Dim columnIndex As Integer
Dim totalElements As Integer
Dim i, col
Dim tempString As String

ReDim ePerRow(r.Columns.Count)
totalElements = 1
i = 0
For Each col In r.Columns
  i = i + 1
  ePerRow(i) = Application.WorksheetFunction.CountA(col)
 totalElements = totalElements * ePerRow(i)
Next

If serialNumber >= totalElements Then
  combinationNo = "Serial number too large"
  Exit Function
End If

tempString = ""
For i = 1 To UBound(ePerRow)
  totalElements = totalElements / ePerRow(i)
  columnIndex = Int(serialNumber / totalElements)
  tempString = tempString & r.Cells(columnIndex + 1, i).Value
  serialNumber = serialNumber - columnIndex * totalElements
Next i

combinationNo = tempString

End Function
使用列所在的范围和序列号(从0开始表示“仅限顶行”)调用此函数。它假定每列的底部都有空格。否则,它将返回一个字符串,该字符串是每列中值组合的串联,正如您所描述的

编辑下面的图片可能会有所帮助,它显示了如何使用该工具以及它的实际用途。请注意,第一个引用(指向不同长度的列的表)是绝对引用(使用
$
符号,因此当您将其从一个单元格复制到另一个单元格时,它始终引用相同的范围),而第二个参数是相对的(因此它依次指向
0、1、2、3
等)


好吧,我迷路了。。。我以前从未使用过VBA。我了解大多数问题,但这一部分:在范围r'中找到每列中的条目数,并选择第n个组合-其中serialNumber=0'给出顶行“假设并非所有列的长度都相同”,但从第一行开始填充…我添加的图片有帮助吗?好的,所以我丢失了。。。我以前从未使用过VBA。我了解大多数问题,但这一部分:在范围r'中找到每列中的条目数,并选择第n个组合-其中serialNumber=0'给出顶行“假设并非所有列的长度都相同”,但从第一行开始填充…我添加的图片有帮助吗?