VBA Excel 2016 循环遍历多个范围 return 偏移值
VBA Excel 2016 Loop through multiple ranges return offset value
问题:
K 和 L 列中有值,取决于 cell/s 是否有值(数字) 我想 return 偏移值 =RC[-4]
以下工作正常:
K4有值,L4有值,什么都不做。
K5有值,L5没有值,value = =RC[-4]
I 运行 当 L 被一个数字覆盖(这是允许的)时出现问题,但是当宏 运行s 时 VBA 仍然覆盖那个数字。例如:
假设 =RC[-4]
等于 20
如果 K4 有值且 L4 为 10
,则跳过此单元格。目前 VBA 会将 L4 中的值覆盖为 20
换个角度来看:
If K4 <> "" And L4 = "" Then "=RC[-4]" Else skip/next cells (K5/L5, K6/L6, etc etc)
这是我想要的输出,但我缺乏研究和知识...
Sub AccrualValue3()
Dim rng As Range
Dim Exrng As Range
Last_Row = Range("H" & Rows.Count).End(xlUp).Row - 1
Set rng = Range("K4:K" & Last_Row)
Set Exrng = Range("L4:L" & Last_Row)
For Each cell In rng
If cell.Value <> "" Then
For Each cell2 In Exrng
If cell2.Value = "" Then
cell.Offset(0, 1).Value = "=RC[-4]"
Else
cell.Offset(0, 1).Value = ""
End If
Next
End If
Next
End Sub
使用 For … To
循环只计算行号会更容易。你也不需要第二个循环。
Option Explicit
Sub AccrualValue3()
Dim LastRow As Long
LastRow = Range("H" & Rows.Count).End(xlUp).Row - 1
Dim iRow As Long
For iRow = 4 To LastRow
If Cells(iRow, "K").Value <> "" And Cells(iRow, "L").Value = "" Then
Cells(iRow, "L").Value = Cells(iRow, "L").Offset(ColumnOffset:=-4).Value
End If
Next iRow
End Sub
或者,您可以 select L 列中的所有空单元格 .SpecialCells(xlCellTypeBlanks)
并仅检查这些单元格的 K 列。如果你有很多行,这应该会更快,因为它只检查 L 列为空的行,而不是 every 行。
Sub AccrualValue3ALTERNATIVE()
Dim LastRow As Long
LastRow = Range("H" & Rows.Count).End(xlUp).Row - 1
Dim EmptyCellsInColumnL As Range
Set EmptyCellsInColumnL = Range("L4:L" & LastRow).SpecialCells(xlCellTypeBlanks)
Dim Cell As Range
For Each Cell In EmptyCellsInColumnL
If Cell.Offset(ColumnOffset:=-1).Value <> "" Then
Cell.Value = Cell.Offset(ColumnOffset:=-4).Value
End If
Next Cell
End Sub
请注意,从最后使用的行中减去 1
LastRow = Range("H" & Rows.Count).End(xlUp).Row - 1
不处理最后使用的行。
问题:
K 和 L 列中有值,取决于 cell/s 是否有值(数字) 我想 return 偏移值 =RC[-4]
以下工作正常:
K4有值,L4有值,什么都不做。
K5有值,L5没有值,value = =RC[-4]
I 运行 当 L 被一个数字覆盖(这是允许的)时出现问题,但是当宏 运行s 时 VBA 仍然覆盖那个数字。例如:
假设 =RC[-4]
等于 20
如果 K4 有值且 L4 为 10
,则跳过此单元格。目前 VBA 会将 L4 中的值覆盖为 20
换个角度来看:
If K4 <> "" And L4 = "" Then "=RC[-4]" Else skip/next cells (K5/L5, K6/L6, etc etc)
这是我想要的输出,但我缺乏研究和知识...
Sub AccrualValue3()
Dim rng As Range
Dim Exrng As Range
Last_Row = Range("H" & Rows.Count).End(xlUp).Row - 1
Set rng = Range("K4:K" & Last_Row)
Set Exrng = Range("L4:L" & Last_Row)
For Each cell In rng
If cell.Value <> "" Then
For Each cell2 In Exrng
If cell2.Value = "" Then
cell.Offset(0, 1).Value = "=RC[-4]"
Else
cell.Offset(0, 1).Value = ""
End If
Next
End If
Next
End Sub
使用 For … To
循环只计算行号会更容易。你也不需要第二个循环。
Option Explicit
Sub AccrualValue3()
Dim LastRow As Long
LastRow = Range("H" & Rows.Count).End(xlUp).Row - 1
Dim iRow As Long
For iRow = 4 To LastRow
If Cells(iRow, "K").Value <> "" And Cells(iRow, "L").Value = "" Then
Cells(iRow, "L").Value = Cells(iRow, "L").Offset(ColumnOffset:=-4).Value
End If
Next iRow
End Sub
或者,您可以 select L 列中的所有空单元格 .SpecialCells(xlCellTypeBlanks)
并仅检查这些单元格的 K 列。如果你有很多行,这应该会更快,因为它只检查 L 列为空的行,而不是 every 行。
Sub AccrualValue3ALTERNATIVE()
Dim LastRow As Long
LastRow = Range("H" & Rows.Count).End(xlUp).Row - 1
Dim EmptyCellsInColumnL As Range
Set EmptyCellsInColumnL = Range("L4:L" & LastRow).SpecialCells(xlCellTypeBlanks)
Dim Cell As Range
For Each Cell In EmptyCellsInColumnL
If Cell.Offset(ColumnOffset:=-1).Value <> "" Then
Cell.Value = Cell.Offset(ColumnOffset:=-4).Value
End If
Next Cell
End Sub
请注意,从最后使用的行中减去 1
LastRow = Range("H" & Rows.Count).End(xlUp).Row - 1
不处理最后使用的行。