Excel 2010 VBA - 获取当前单元格的左侧单元格?

时间:2013-06-29 10:28:18

标签: excel excel-vba excel-2010 vba

我的表格是这样的:

| 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

我该怎么做?

3 个答案:

答案 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)