2

我的表是这样的:

| 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

我该怎么做呢?

4

3 回答 3

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
于 2013-06-29T10:41:42.590 回答
3

您可以在不使用循环的情况下执行此操作Evaluate

Sub QuickKill()
Range("A1:A1000") = Application.Evaluate("=IF(B1:B1000<>"""",B1:B1000,A1:A1000)")
End Sub
于 2013-06-29T12:32:54.993 回答
0

使用此代码

For each c in range("a2"a8")
c.value= c & " " & c.offset(,1)
next c
columns(2).delete

上面的代码将结合两列

c.value= c.offset(,1)
于 2013-06-29T10:49:34.980 回答