【问题标题】:Variable Combination Writer变量组合写入器
【发布时间】:2018-05-26 12:15:36
【问题描述】:

我有一个 VBA 程序,它应该创建并写出多达 7 个不同变量的完整组合,每个变量都有不同的级别。

代码循环然后所有组合并在每个变量之间用空格写入它们。它首先按最后一行 (LineP) 组织,然后从第一行到最后一行 (Line1 到 Line 6)。

到目前为止,代码仍然有效,除了如果用户将一行留空,它会假定没有组合,因为数组是空的。 我可以通过将 数组定义为 " " 来解决这个问题,但是这样会在组合中的变量之间留下额外的两个空格。 代码现在的工作方式不仅涉及不在变量的位置写入任何内容,而且还涉及删除空间。

每个变量的不同级别存储在一个数组中(变量 1 的级别在 Array1 中,变量 P 的级别在 ArrayP 中,等等)。下面是我目前用来写出每个组合的代码:

`'Create Label Combinations
If Rowi > 1 Then
    Dim Labeli As String
    Dim Rowi2 As Integer
    Rowi2 = Rowi
    If P = 1 Then
        For iP = 0 To UBound(ArrayP)
            For i1 = 0 To UBound(Array1)
                For i2 = 0 To UBound(Array2)
                    For i3 = 0 To UBound(Array3)
                        For i4 = 0 To UBound(Array4)
                            For i5 = 0 To UBound(Array5)
                                For i6 = 0 To UBound(Array6)
                                  Labeli = Array1(i1) & " " & Array2(i2) & _ 
                                         " " & Array3(i3) & " " & _  
                                           Array4(i4) & " " & Array5(i5) & _ 
                                         " " & Array6(i6) & " " & ArrayP(iP)
                                    Cells(Rowi2, 1).Value = Labeli
                                    Rowi2 = Rowi2 + 1
                                Next i6
                            Next i5
                        Next i4
                    Next i3
                Next i2
            Next i1
        Next iP
    End If
End If`

这里是当前输出的一个例子:

由于每次使用的变量数量和每个变量的级别都会发生变化,我不确定是否可以使用多维数组来解决这个问题。我认为可以在“Labeli”字符串中嵌入一个 if 语句,但我没有发现任何暗示这是可能的。任何帮助将非常感激。谢谢!

【问题讨论】:

    标签: excel combinations vba


    【解决方案1】:

    我已经尝试并测试了以下内容,它可以满足您的期望:

    Private Sub CommandButton1_Click()
    LastRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
    Rowi = LastRow + 1
    If TextBox1.Text <> "" Then
        TempArray1 = Split(TextBox1.Text, ",")
    Else
        TempArray1 = Array(" ")
    End If
    
    If TextBox2.Text <> "" Then
        TempArray2 = Split(TextBox2.Text, ",")
    Else
        TempArray2 = Array(" ") 'if text box is empty add a space to the array (we'll remove the space later)
    End If
    
    If TextBox3.Text <> "" Then
        TempArray3 = Split(TextBox3.Text, ",")
    Else
        TempArray3 = Array(" ")
    End If
    
    If TextBox4.Text <> "" Then
        TempArray4 = Split(TextBox4.Text, ",")
    Else
        TempArray4 = Array(" ")
    End If
    
    If TextBox5.Text <> "" Then
        TempArray5 = Split(TextBox5.Text, ",")
    Else
        TempArray5 = Array(" ")
    End If
    
    If TextBox6.Text <> "" Then
        TempArray6 = Split(TextBox6.Text, ",")
    Else
        TempArray6 = Array(" ")
    End If
    
    If TextBox7.Text <> "" Then
        TempArray7 = Split(TextBox7.Text, ",")
    Else
        TempArray7 = Array(" ")
    End If
    
    For i1 = 0 To UBound(TempArray1)
        For i2 = 0 To UBound(TempArray2)
            For i3 = 0 To UBound(TempArray3)
                For i4 = 0 To UBound(TempArray4)
                    For i5 = 0 To UBound(TempArray5)
                        For i6 = 0 To UBound(TempArray6)
                            For i7 = 0 To UBound(TempArray7)
                                Labeli = TempArray1(i1) & " " & TempArray2(i2) & " " & TempArray3(i3) & " " & TempArray4(i4) & " " & TempArray5(i5) & " " & TempArray6(i6) & " " & TempArray7(i7)
                                Sheet1.Cells(Rowi, 1).Value = Trim(Labeli) 'Change Sheet1 to your Sheets("YourSheetName") or to ActiveSheet
                                Rowi = Rowi + 1
                            Next i7
                        Next i6
                    Next i5
                Next i4
            Next i3
        Next i2
    Next i1
    SpaceKiller 'call spacekiller function to remove all the extra spaces
    End Sub
    
    Sub SpaceKiller()
       Worksheets("Sheet1").Columns("A").Replace _
          What:="  ", _
          Replacement:=" ", _
          SearchOrder:=xlByColumns, _
          MatchCase:=True
    'Change Sheet1 to your Sheets("YourSheetName") or to ActiveSheet
       Set r = Worksheets("Sheet1").Columns("A").Find(What:="  ")
       If r Is Nothing Then
       Else
          Call SpaceKiller
       End If
    End Sub
    

    【讨论】:

    • 哦,是的,这样会更有意义。谢谢!
    • 不幸的是,如果数组为空,则组合循环不会写入任何内容,因为它写入的是iX = 0 to UBound(ArrayX)。你知道我该如何解决这个问题吗?
    • @YTYT,我已经尝试并测试了更新的答案,请检查它,因为我相信它应该可以满足您的要求。另外,如果这对您有帮助,您能否将我的回复标记为答案?谢谢。
    • 这个答案不允许用户在其中一行中放一个空格,对吧?我使用了您最初的想法,即连接数组以形成标签来解决问题。如果数组为空白,我将其定义为“空白”,然后将变量设置为 1,表示该数组为空白。在将数组添加到标签之前,程序会检查数组中的点是否为空白以及空白变量是否等于 1。如果其中任何一个为真,则不会将该数组添加到标签中。
    • 它确实允许用户键入一个空格,但它不会使用该空格在标签上创建一个双空格“”,因为 spacekiller sub 将继续循环,直到所有双空格都替换为指定范围内的单个空格“”...
    猜你喜欢
    • 1970-01-01
    • 2011-01-18
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多