Excel VBA特定的行复制粘贴通过单元格引用

时间:2019-04-18 10:49:53

标签: excel vba

我必须从这么多行中选择特定行,然后将粘贴复制到另一张纸上,但是如果它要从特定单元格中获取值,我就不想指定该单元格的值
我的代码是

SET NOCOUNT ON

我想从Urban-Ward_vise中选择行并粘贴到sheet1

Sub Macro3()
    Dim rngG As Range
    Dim cell As Range
    Sheets("Urban-Ward_vise").Select
    Set rngG = Range("G1", Range("G65536").End(xlUp))
    For Each cell In rngG
        If cell.Value = "BAVLA" Then
            cell.EntireRow.Copy
            Sheets("Sheet1").Select
            ActiveCell.Select
            ActiveCell.Offset(1, 0).Range("A1").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
        End If
    Next cell
    Selection.End(xlUp).Select

End Sub

我不想放值“ BAVLA”,但我想从sheet1单元格(如c5)中读取值,所以请帮忙

1 个答案:

答案 0 :(得分:0)

你的意思是这样吗?

您值得阅读how to avoid Select

Sub Macro3()

Dim rngG As Range
Dim cell As Range

With Sheets("Urban-Ward_vise")
    Set rngG = .Range("G1", .Range("G" & Rows.Count).End(xlUp))
End With

For Each cell In rngG
    If cell.Value = Sheets("Sheet1").Range("C5").Value Then 'specify sheet name and cell here
        cell.EntireRow.Copy
        Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp)(2).PasteSpecial Paste:=xlPasteValues
    End If
Next cell

End Sub