![](/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.