Excel 2010 VBA - 获取当前单元格的左侧单元格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17379221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel 2010 VBA - Get the left side cell of the current cell?
提问by ComputerFellow
My sheet is like this:
我的表是这样的:
| A | B | ...
---------
| 1 | a | ...
---------
| 2 | |
---------
| 3 | |
---------
| 4 | b |
---------
And I want this as my output:
我希望这是我的输出:
| A | B | ...
---------
| a | | ...
---------
| 2 | |
---------
| 3 | |
---------
| b | |
---------
So I'm looping column B
and whenever there's a value in B
then I should replace the corresponding one in A
所以我在循环列B
,每当有一个值时,B
我应该替换相应的值A
So far I've tried like this:
到目前为止,我已经尝试过这样的:
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
How do I do this?
我该怎么做呢?
回答by ComputerFellow
Solved it! But check out brettdj's answer. I guess it's better.
解决了!但是请查看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
回答by brettdj
You could do this without loops utilising Evaluate
:
您可以使用以下方法在没有循环的情况下执行此操作Evaluate
:
Sub QuickKill()
Range("A1:A1000") = Application.Evaluate("=IF(B1:B1000<>"""",B1:B1000,A1:A1000)")
End Sub
回答by Siva S Tenet
Use this code
使用此代码
For each c in range("a2"a8")
c.value= c & " " & c.offset(,1)
next c
columns(2).delete
The Above code will combine the two columns
上面的代码将合并两列
c.value= c.offset(,1)