繁体   English   中英

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

[英]Excel VBA if Column B contains a value then copy that value to Column A do not overwrite existing Column A data

我现在有一些代码可以查看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

我想转一下:

StartTable

到这个:

茶几

在此先感谢您的协助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM