【问题标题】:Copy paste values within cell range在单元格范围内复制粘贴值
【发布时间】:2018-05-12 08:45:14
【问题描述】:

继我之前的问题之后,我现在想在单元格范围内复制粘贴值。

我之前的查询中使用的代码是;

Sub CopyYesInW()
Dim lastRow As Long, i As Long
'determine last row in column W
lastRow = Cells(Rows.Count, "W").End(xlUp).Row
For i = 1 To lastRow
    'if Yes in W then copy from P to W in current row
    If Cells(i, "W").Value = "Yes" Then
        Cells(i, "P").Value = Cells(i, "P").Value
    End If
    If Cells(i, "W").Value = "Yes" Then
        Cells(i, "U").Value = Cells(i, "U").Value
    End If
Next
End Sub

我已经修改了下面脚本中的代码,以检查单元格范围 C6:N6 的值 = Yes,然后将值复制到 C9:N9 中的单元格上。但是我不确定我做错了什么。存在运行时错误“5”无效过程调用或参数

Sub CopyYesInForecast()
Dim lastRow As Long, i As Long
'determine last row in column W
lastRow = Cells("C6")
For i = 1 To lastRow
'if Yes in W then copy from P to W in current row
    If Cells(i, "C6:N6").Value = "Yes" Then
        Cells(i, "C9:N9").Value = Cells(i, "C9:N9").Value
    End If
Next
End Sub

【问题讨论】:

  • 您可以通过使用宏记录操作然后编辑以查看生成的代码来获取复制/粘贴范围的代码。 (见thisthis。)在那之后,听起来你只需要一个简单的IF 语句。
  • 第一个代码有什么问题?为什么不把问题说清楚,只显示第二个代码示例并清楚说明错误是什么?
  • @pnuts 存在运行时错误 '5' 无效的过程调用或参数
  • 我建议你谷歌或在 VBA 帮助中查找单元格和范围语法。

标签: vba excel range copy-paste


【解决方案1】:

虽然您的叙述很薄弱,但也许这与您正在尝试的内容很接近。

Sub CopyYesInForecast()
    Dim c As Long

    For c = range("C:C").column to range("N:N").column
        If Cells(6, c).Value = "Yes" Then
            Cells(9, c) = Cells(6, c).Value
        End If
    Next
End Sub

这会遍历第 6 行,C 列到 N 列,如果找到 Yes,则将值传输到第 9 行的同一列。

【讨论】:

  • 感谢,对问题描述不佳表示歉意
猜你喜欢
  • 2018-09-06
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多