根据等于定义名称的列中的值复制行

时间:2017-10-19 07:47:53

标签: excel-vba vba excel

我正在尝试创建一个宏,它将根据C列中的值等于我定义的一个名称(它是一个日期)来复制整行,然后,它会将值精确地粘贴在同一个地方 - 冻结功能。

这是我到目前为止所做的,但是一旦运行代码就没有响应(没有错误,几乎没有)。我一直在这里研究类似的主题,下面是我发现与我想要的相匹配的混合物:

Sub testIt()
Dim r As Long, endRow As Long, pasteRowIndex As Long

endRow = 1000 ' preferably change for a function that will retrieve the last used row number via a function

For r = 7 To endRow 'Loop through sheet1 and search for your criteria

    If Cells(r, Columns("C").Column).Value = ThisWorkbook.Names("MyDate").Value Then Rows(r).Select

    For Each cell In Selection
    cell.Value = cell.Value
    Next cell
    End If
Next r
End Sub

我会非常感谢一些提示。

1 个答案:

答案 0 :(得分:1)

当我测试它时,我发现名称的值返回其地址,而不是其实际值,因此请使用Range。此外,没有必要选择和循环每个单元格(虽然我怀疑你需要在整行上操作)。

Sub testIt()

Dim r As Long, endRow As Long, pasteRowIndex As Long

endRow = 1000 ' preferably change for a function that will retrieve the last used row number via a function

For r = 7 To endRow 'Loop through sheet1 and search for your criteria
    If Cells(r, 3).Value = Range("MyDate").Value Then
        Rows(r).Value = Rows(r).Value
    End If
Next r
'Alternatively you could use ThisWorkbook.Names("MyDate").Referstorange.Value
End Sub