簡體   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