Excel VBA如果列B包含一个值,则将该值复制到列A不会覆盖现有的列A数据

我现在有一些代码将看看列B,并将更新列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 

我想把这个变成:

StartTable

进入这个:

茶几

在此先感谢您的帮助!

您可以使用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