我的表格是这样的:
| A | B | ...
---------
| 1 | a | ...
---------
| 2 | |
---------
| 3 | |
---------
| 4 | b |
---------
我希望这是我的输出:
| A | B | ...
---------
| a | | ...
---------
| 2 | |
---------
| 3 | |
---------
| b | |
---------
所以我循环列B
,只要B
中有值,我就应该替换A
中的相应值
到目前为止,我尝试过这样:
Sub LoopRange()
Dim rCell As Range
Dim rRng As Range
Set rRng1 = Sheet1.Range(A1, A1000)
Set rRng2 = Sheet1.Range(B1, B1000)
For Each rCell In rRng2.Cells
If Not IsEmpty(rCell.Value) Then
'SET THE VALUE OF THIS rCELL TO THE CELL THAT'S LEFT OF IT
Next rCell
End Sub
我该怎么做?
答案 0 :(得分:5)
解决了!但请查看 brettdj 的回答。我想这更好。
Sub LoopRange()
Dim rCell As Range
Dim rRng As Range
Set rRng = Sheet1.Range("B1:B1000")
For Each rCell In rRng.Cells
If Not IsEmpty(rCell.Value) Then
rCell.Offset(0, -1) = rCell.Value
End If
Next rCell
End Sub
答案 1 :(得分:3)
您可以在不使用Evaluate
的循环的情况下执行此操作:
Sub QuickKill()
Range("A1:A1000") = Application.Evaluate("=IF(B1:B1000<>"""",B1:B1000,A1:A1000)")
End Sub
答案 2 :(得分:0)
使用此代码
For each c in range("a2"a8")
c.value= c & " " & c.offset(,1)
next c
columns(2).delete
以上代码将合并两列
c.value= c.offset(,1)