搜索标题,将粘贴值复制到最后一行

时间:2018-06-06 05:04:40

标签: excel vba excel-vba

我创建了一个宏来搜索标题并复制标题并将其粘贴到该特定列的最后一行。但是当我这样做时,我必须指定我不想要的列。但是我需要在同一列中将Header搜索粘贴到最后一行。例如:总计是BV列中的标题名称。请协助。

Range("A1").Select
Dim rngUsernameHeader As Range
Dim rngHeaders As Range
Set rngHeaders = Range("1:1")
Set rngUsernameHeader = rngHeaders.Find(what:="Total", After:=Cells(1, 1))
rngUsernameHeader.Copy
lastrow = Range("A65536").End(xlUp).Row
**ActiveSheet.Paste Destination:=Range("BV1:BV" & lastrow)**
Selection.End(xlUp).Select
Application.CutCopyMode = False

3 个答案:

答案 0 :(得分:1)

只需从rngUsernameHeader

构建您的粘贴范围
ActiveSheet.Paste Destination:=Range( _
  Cells(1, rngUsernameHeader.Column), _
  Cells(lastrow, rngUsernameHeader.Column))

答案 1 :(得分:1)

这就是你要找的东西吗?

Range("A1").Select
Dim rngUsernameHeader As Range
Dim rngHeaders As Range
Set rngHeaders = Range("1:1")
Set rngUsernameHeader = rngHeaders.Find(what:="Total", After:=Cells(1, 1))
rngUsernameHeader.Copy
lastrow = Range("A65536").End(xlUp).Row
ActiveSheet.Paste Destination:=Range(rngUsernameHeader, rngUsernameHeader.Offset(lastrow - 1))

Selection.End(xlUp).Select
Application.CutCopyMode = False

答案 2 :(得分:0)

如果要将第1行中包含“Total”的第1行单元格的内容从第1行粘贴到与A列对应的行,最后不是空的,请使用:

Sub main()
    With Range("1:1").Find(what:="Total", After:=Cells(1, 1))
        .Resize(Cells(Rows.Count, 1).End(xlUp).Row).Value = .Value
    End With
End Sub

或者,如果您知道“总计”是标题的全部内容

Sub main()
    Range("1:1").Find(what:="Total", After:=Cells(1, 1)).Resize(Cells(Rows.Count, 1).End(xlUp).Row).Value = "Total"
End Sub

同时,如果要将第1行中包含“Total”的第1行单元格的内容从第1行粘贴到同一列的最后一个非空单元格,请使用:

Sub main2()
    With Range("1:1").Find(what:="Total", After:=Cells(1, 1))
        .Resize(Cells(Rows.Count, .Column).End(xlUp).Row).Value = .Value
    End With
End Sub