简体   繁体   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

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: 我想转一下:

StartTable

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.

相关问题 Excel VBA-如果B列中的单元格包含一个值,则A列等于“值”,但不要覆盖现有的A列数据 - Excel VBA - If a cell in column B contains a value then column A equals “value” BUT do not overwrite existing column A data Excel VBA-如果列B包含任何值,则使用值更新列A。 如果列B不包含任何值,则不要运行宏 - Excel VBA - Update column A with a value if column B contains any value. If column B contains no values then do not run the macro 如果列B在Excel中包含正确的值,则突出显示列A中的单元格 - Highlighting cells in column A if column B contains the correct value in Excel Excel VBA:将数据复制到具有值的最后一列 - Excel VBA: Copy the Data up to the last column that has value 如果A列包含文本,则将值从B列中的单元格复制到B列中的相应项目 - If column A contains text, copy value from cell in Column B to corresponding item in Column B Excel VBA - 自动筛选后从 B 列开始复制数据 - Excel VBA - Copy data after autofiltering starting with column B VBA如果A列中的值然后在B列中替换 - VBA if value in column A then replace in column B Excel VBA —查找列A中任何值的第一个出现位置,然后将列B的值插入列C(同一行) - Excel VBA — Find first occurrence of any value in column A, then insert value of column B into column C (same row) Excel VBA在列中查找值并进行数学运算 - Excel VBA Find value in column, and do math Excel VBA-仅复制在其他工作簿上的列中包含特定值的行范围 - Excel VBA - copy only row-range that contains specific value in column on a different workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM