excel - 尝试根据标准将列中单元格的值移动到另一个

标签 excel vba

我在下面有这段代码,我想做什么搜索column D如果有 无值(value)然后取 column c 中的值并放入column D .

不幸的是,当我运行它时,它只会删除 C 列中的所有内容。

Sub CleanReportStep10() 
'align columns C and D realign columns by moving data from C to D'
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("copied") 
Dim i As Long

For i = 19 To ws.Range("C" & ws.Rows.Count).End(xlUp).Row
    If ws.Range("D" & i) = "" Then
        ws.Range("C" & i).Copy.Value = ws.Range("D" & i).Value
    End If
Next i

End Sub

最佳答案

将您的声明更改为

ws.Range("D" & i).Value = ws.Range("C" & i).Value

在这种情况下,循环遍历一个范围可能会更快一些。此外,切换 ScreenUpdating从用户的角度使这个更清洁
Option Explicit

Sub Alternative()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("copied")
Dim LRow As Long, MyCell As Range

LRow = ws.Range("C" & ws.Rows.Count).End(xlUp).Row

Application.ScreenUpdating = False
    For Each MyCell In ws.Range("C19:C" & LRow)
        If MyCell.Offset(, 1) = "" Then MyCell.Offset(, 1).Value = MyCell.Value
    Next MyCell
Application.ScreenUpdating = True

End Sub

关于excel - 尝试根据标准将列中单元格的值移动到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52877347/

相关文章:

vba - Excel 2010 VBA - 使用变量定义的范围更新图形

c# - 无法读取 Excel 文件 - File.open 无效

excel - 在 Visual Basic 中从用户窗体中获取单个变量

Excel VBA 按字母顺序循环遍历范围

excel - 引用 Excel 时,AutoCAD 中的 VBA 错误不一致

vba - Excel 2013 中的扩展工作表选择 - VBA

vba - 我尝试在 VBA 中编写代码以将此公式插入单元格时一直失败

vba - 在excel vba数组中查找唯一日期

excel - 引用公式中的单元格地址/引用

vba - 公共(public)动态数组返回错误