【问题标题】:Print array values a variable number of times以可变次数打印数组值
【发布时间】:2017-04-14 22:39:00
【问题描述】:

我正在尝试编写一个宏,该宏将根据其他单元格中的条件打印出数组中的值。我已经让宏打印出数组中的一个值,而不是其他值。电子表格如下所示:

Column 1 | Column 2
___________________
L1       |
L1       |
L2       |
L3       |
L1       |
L5       |
L1       |

数组看起来像这样: List = Array("Person1", "Person2", "Person3") 我想要做的是打印 Person1、Person2 等,每个值表示 L1 到最后一个 L1价值。它应该类似于下面的示例。

Column 1 | Column 2
___________________
L1       | Person1
L1       | Person2
L2       |
L3       |
L1       | Person3
L5       |
L1       | Person1

下面的宏部分工作,但它只打印一个人,Person3。任何帮助将不胜感激!

Sub Practice()

Dim i, j, k As Integer
Dim List As Variant
Dim LastRow As Long, CountL As Long
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")
List = Array("Person1", "Person2", "Person3")

LastRow = ws.Cells(Rows.Count, "C").End(xlUp).Row - 1

For i = LBound(List) To UBound(List)
For j = 2 To LastRow
    If ws.Cells(j, 3).Value = "L1" Then
        ws.Cells(j, 4) = List(i)
    Else 'Do Nothing
    End If
Next j
Next i

End Sub

请注意,实际电子表格中的“L”值在 C 列中,人名在 D 列中,这就是为什么宏中的列与我在此处添加的示例数据中的列不匹配的原因。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    看看下面的例子:

    Sub Practice()
    
        Dim ws As Worksheet
        Dim List As Variant
        Dim LastRow As Long
        Dim i As Integer
        Dim j As Integer
    
        Set ws = ThisWorkbook.Sheets("Sheet1")
        List = Array("Person1", "Person2", "Person3")
    
        LastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
    
        i = 0
        For j = 2 To LastRow
            If ws.Cells(j, 3).Value = "L1" Then
                ws.Cells(j, 4) = List(i Mod 3)
                i = i + 1
            End If
        Next
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      您的代码当前正在对列表中的每个值重复其操作,并且每次迭代都会为每个 L1 行分配一个值,并覆盖上一次迭代中写入的内容。

      您实际上需要保留一个计数器,以记录接下来要写入的数组中的哪个值:

      Sub Practice()
          'You should declare the type of each variable, or else they will be Variant
          'Dim i, j, k As Integer
          Dim i As Integer, j As Integer, k As Integer
          Dim List As Variant
          Dim LastRow As Long, CountL As Long
          Dim ws As Worksheet
      
          Set ws = ActiveWorkbook.Sheets("Sheet1")
          List = Array("Person1", "Person2", "Person3")
      
          'You should fully qualify objects such as Range, Cells and Rows
          'LastRow = ws.Cells(Rows.Count, "C").End(xlUp).Row - 1
          LastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row '<-- not sure why you subtracted 1
      
          i = LBound(List)
          For j = 2 To LastRow
             If ws.Cells(j, 3).Value = "L1" Then
                 ws.Cells(j, 4) = List(i)
                 i = i + 1
                 If i > UBound(List) Then
                     i = LBound(List)
                 End If
             End If
          Next j
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2020-10-04
        • 1970-01-01
        • 1970-01-01
        • 2015-02-10
        • 1970-01-01
        • 2013-08-31
        • 2021-07-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多