[英]Excel VBA if Column B contains a value then copy that value to Column A do not overwrite existing Column A data
I have some code right now that will look at Column B and will update Column A with a predetermined value ("ANY VALUE" from my code snippet) but what I am looking for is to be able to copy what is in Column B and paste it into Column A. Here is the code I have so far: 我现在有一些代码可以查看B列,并使用预定值(代码片段中的“ ANY VALUE”更新A列),但是我要寻找的是能够复制B列中的内容并粘贴将其放入A列。这是我到目前为止的代码:
Sub Copy_And_Paste_Column_Values_Into_Column_1()
On Error Resume Next
Dim ws As Worksheet
Dim lRow As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
lRow = .Range("B" & .Rows.Count).End(xlUp).Row
.Range("A1:A" & lRow).SpecialCells(xlCellTypeBlanks).Formula = "=If(B1<>"""",""ANY VALUE"","""")"
.Range("A1:A" & lRow).Value = .Range("A1:A" & lRow).Value
End With
End Sub
I would like to turn this: 我想转一下:
into this: 到这个:
Thanks in advance for the assistance! 在此先感谢您的协助!
You can do this a little more succinctly, using SpecialCells()
: 您可以使用
SpecialCells()
来SpecialCells()
:
Sub copy_data_to_blanks()
Dim rng As Range
Set rng = Range("A1:A3") ' Change this as necessary.
rng.Cells.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=rc[1]"
rng.Value = rng.Value ' This effectively removes the formulas, just by overwriting the range with the actual values.
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.