[英]VBA For each cell in
I have a sheet with data in columns A to H. Some cells in column A and B have no values or are empty, but there are values in that rows other columns that is C to H. Now I have this code to do this. 我有一个工作表,其中数据包含在A到H列中。A和B列中的某些单元格没有值或为空,但其他行中的值存在其他列,即C到H。现在我有这段代码可以执行此操作。 loop through each empty cell in B and put in the values.
循环遍历B中的每个空单元格并输入值。 that is my code will fill down till the last non empty cell in B. but I get an error.
那就是我的代码将填满直到B中的最后一个非空单元格,但是我得到一个错误。
Sub filldownemptyAB()
Dim cel As Range, rng As Long
rng = Cells(Rows.Count, 1).End(xlUp).row
For Each cel In rng.Cells
With ActiveCell
.Offset(, 0).Formula = "=year(today())"
.Offset(, -1).Value = "Actual"
End With
Next cel
End Sub
the error is at this line 错误在这一行
For each cel in rng
I believe you're looking for this: 我相信您正在寻找这个:
(I edited a few times to simplify things a bit and to scan column B
instead of column A
) (我编辑了几次,以简化操作并扫描
B
列而不是A
列)
Sub FillDownEmptyAB()
Dim c, lr
lr = ActiveSheet.UsedRange.Rows.CountLarge
For Each c In Range("B1:B" & lr)
If c.Value = "" Then
c.Offset(,-1).Value = "Actual"
c.Formula = "=YEAR(TODAY())"
End If
Next c
End Sub
Input: 输入:
Output: 输出:
A Long
is not a collection of Ranges
. Long
不是Ranges
的集合。
I think you are trying to do something like this: 我认为您正在尝试执行以下操作:
Sub filldownemptyAB()
Dim cel As Range
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).row
Dim rng as Range
Set rng = Range("B" & lastRow)
For Each cel In rng
With cel
.Offset(, 0).Formula = "=year(today())"
.Offset(, -1).Value = "Actual"
End With
Next cel
End Sub
But it's hard to tell. 但这很难说。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.