如果可以在没有VBA的情况下执行此语法,这就是理论上的Excel语法:
IF(立即离开的单元格=有色(或已填充),将内容复制并粘贴到此单元格,复制并粘贴上面单元格的内容进入这个单元格)。
我如何在VBA中执行此操作?
答案 0 :(得分:0)
你必须编写周围的代码来遍历你想要评估的单元格,但它的关键是这样的:
If Range("A2").Interior.ColorIndex > xlNone Then
Range("B2") = Range("A2").Value
Else
Range("B1") = Range("A2").Value
End If
循环可以是For循环,可能类似于:
For i = 2 to <whatever>
If Range("A" & i).Interior.ColorIndex > xlNone Then
Range("B" & i) = Range("A" & i).Value
Else
Range("B" & i - 1) = Range("A" & i).Value
End If
Next i
答案 1 :(得分:0)
您可以在VBA中使用UDF:
Function Color(MyCell As Range)
If MyCell.Interior.ColorIndex > 0 Then
Result = 1
Else
Result = 0
End If
Color = Result
End Function
然后在 B2 中使用以下公式来填充单元格:
=IF(color(A2)=1,A2,B1)
希望这适合你。
答案 2 :(得分:0)
作为输入Cell以评估填充的UDF:
Function CellColored(rng As Range) As Variant
If rng.Interior.ColorIndex > xlNone Then
CellColored = rng.Value2
Else
CellColored = Empty
End If
End Function
作为没有输入的UDF,只需假设条件单元格位于调用单元格的右侧:
Function CellColored() As Variant
If Application.Caller.Offset(0, 1).Interior.ColorIndex > xlNone Then
CellColored = Application.Caller.Offset(0, 1).Value2
Else
CellColored = Empty
End If
End Function
然后,您可以在第二个版本中将该函数调用为CellColored(A2)
或CellColored()
。欢呼声,