【问题标题】:EXCEL VBA - Loop through cells in a row, if not empty, copy cell value into another row until next row not emptyEXCEL VBA - 循环遍历一行中的单元格,如果不为空,则将单元格值复制到另一行,直到下一行不为空
【发布时间】:2016-11-11 04:28:00
【问题描述】:

我正在考虑编写一些代码将行的数据复制到下一行,直到下一行不为空。假设第 1 行中的所有内容都是标题。

数据是这样的:

    A2 ClientA       B2 Product1       C2 Price       D2 Quantity       E2 Cost
    A3               B3                C3 Price       D3 Quantity       E3 Cost
    A4               B4                C4 Price       D4 Quantity       E4 Cost
    A5 ClientB       B5 Product2       C5 Price       D5 Quantity       E5 Cost
    A6               B6                C6 Price       D6 Quantity       E6 Cost
    A7 ClientC       B7 Product3       C7 Price       D7 Quantity       E7 Cost
    A8               B8                C8 Price       D8 Quantity       E8 Cost
    A9               B9                C9 Price       D9 Quantity       E9 Cost

A3 和 A4 将用 ClientA 填充,B3 和 B4 将用 Product 填充,直到下一行不为空。

我找了一个类似的问题,但是不知道怎么写。

EXCEL VBA - Loop through cells in a column, if not empty, print cell value into another column

最终结果如下:

    A2 ClientA       B2 Product1       C2 Price       D2 Quantity       E2 Cost
    A3 ClientA       B3 Product1       C3 Price       D3 Quantity       E3 Cost
    A4 ClientA       B4 Product1       C4 Price       D4 Quantity       E4 Cost
    A5 ClientB       B5 Product2       C5 Price       D5 Quantity       E5 Cost
    A6 ClientB       B6 Product2       C6 Price       D6 Quantity       E6 Cost
    A7 ClientC       B7 Product3       C7 Price       D7 Quantity       E7 Cost
    A8 ClientC       B8 Product3       C8 Price       D8 Quantity       E8 Cost
    A9 ClientC       B9 Product3       C9 Price       D9 Quantity       E9 Cost

有人知道 VBA 代码是什么吗?

【问题讨论】:

    标签: vba excel for-loop


    【解决方案1】:

    如果你想使用 VBA,你可以使用这个。

    我不确定你想如何找到最后一行的数据,所以我提供了一个你可以自己更改的变量,它对应于该行。第一个使用 Z 的 For 循环将遍历 A(1) 列和 B(2) 列,在这个循环中,我们有另一个 For 循环遍历单元格,如果单元格值为 "",则输入最后看到的值。

    Sub LoopFill()
    
    Dim LastEntry As String, LastRow As Integer
    
    LastRow = 25
    
    For Z = 1 To 2
        LastEntry = ""
        For i = 1 To LastRow
            If Cells(i, Z).Value = "" Then Cells(i, Z).Value = LastEntry
            LastEntry = Cells(i, Z).Value
        Next i
    Next Z
    
    End Sub
    

    【讨论】:

    • 如果我想找到 E 列的最后一行呢?添加喜欢: LastRow = activesheet.Cells(.Rows.Count, "E").End(xlUp).Row
    • 如果您发现其中一个答案有帮助或回答了您的问题,请将其标记为答案。
    【解决方案2】:

    这是不使用 VBA 的另一种方法。

    1. 选择空白(CTRL+G,单击特殊,选择空白,确定)
    2. 在公式栏中,键入 =(CELL DIRECTLY ABOVE THE FIRST BLANK) 例如:如果 A2 为空白,=A1
    3. 按住 CTRL,按 Enter 将公式提交到所有空白单元格。
    4. 如有必要,选择范围、复制和粘贴值以删除公式。
    5. 如果您不粘贴值,并尝试进行排序或过滤,则数据将出错

    【讨论】:

      【解决方案3】:

      此操作不需要 VBA。

      将此公式写入帮助列中:=IF(ISBLANK(A2),F1,A2)

      假设辅助列位于 F 列中。将其拖到数据集下方,然后将值复制到原始列中。对 B 列重复此操作。

      【讨论】:

      • 我发现即使某些 C 列是空的,这也能正常工作,谢谢 mate
      【解决方案4】:

      您需要:

      • SheetName:要处理的工作表的名称

      • arColumns = Array("A", 2, 3):将要填充的列号或字母添加到此数组中


      子填充行() Const SheetName As String = "Sheet3" 暗淡 lastRow As Long, x As Long, y DiarColumns arColumns = Array("A", 2, 3) 使用工作表(SheetName) lastRow = .Rows(Rows.Count).End(xlUp).Row 对于 x = 3 到 lastRow 对于 y = 0 到 UBound(arColumns) If IsEmpty(.Cells(x, arColumns(y)).value) Then .Cells(x, arColumns(y)).value = .Cells(x - 1, arColumns(y)).value 下一个 下一个 结束于 结束子

      【讨论】:

      • UBound(arColumns) 是找到数组的上限,但是数组声明不是动态的,我明白了吗? arColumns = Array("A", 2, 3) 那么代码是如何工作的呢?
      • UBound(arColumns)UBound(arColumns, 1) 返回数组第一个维度的最后一个索引。
      猜你喜欢
      • 2015-01-16
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多