![](/img/trans.png)
[英]Excel VBA - Copy Cell Value, if empty, to another Cell in the same row
[英]EXCEL VBA - Loop through cells in a row, if not empty, copy cell value into another row until next row not empty
我正在考虑编写一些代码以将行的数据复制到下一行,直到下一行不为空。 假设第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-遍历列中的单元格,如果不为空,则将单元格值打印到另一列中
最终结果如下:
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,则可以使用它。
我不确定您想如何找到包含数据的最后一行,因此我提供了一个可以更改自己的变量,它对应于该行。 第一个使用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
这是没有VBA的另一种方法。
不需要VBA。
将此公式写在帮助器列中: =IF(ISBLANK(A2),F1,A2)
假定帮助程序列在F列中。将其向下拖动到数据集中,然后将值复制到原始列中。 重复B列。
您需要:
SheetName:到要处理的工作表的名称
arColumns = Array(“ A”,2,3):将要填充的列号或字母添加到此数组
Sub FillRows() Const SheetName As String = "Sheet3" Dim lastRow As Long, x As Long, y Dim arColumns arColumns = Array("A", 2, 3) With Worksheets(SheetName) lastRow = .Rows(Rows.Count).End(xlUp).Row For x = 3 To lastRow For y = 0 To UBound(arColumns) If IsEmpty(.Cells(x, arColumns(y)).value) Then .Cells(x, arColumns(y)).value = .Cells(x - 1, arColumns(y)).value Next Next End With End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.