选择&在第1列中使用空单元格复制过滤范围

时间:2016-09-15 17:07:57

标签: excel vba excel-vba

此代码似乎将数据粘贴到工作表HAA的第11行,只需要在工作表HAA上附加

Sub Run()

Application.ScreenUpdating = False

Dim x As Long
Dim rf As Range, wsTo As Worksheet, wx As Range

Set rf = ThisWorkbook.Sheets("Table").UsedRange
Set wsTo = Sheets("HAA")
Set wx = ThisWorkbook.Sheets("HAA").UsedRange

x = Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Row

rf.AutoFilter
rf.AutoFilter 12, "associated"
rf.Copy

wsTo.Range("A1").PasteSpecial xlPasteValues

rf.AutoFilter
rf.AutoFilter 12, "not found"
rf.Offset(1, 0).Copy

'我将其更改为.select以查看所选内容 最初是.PasteSpecial xlPasteValues

wsTo.Range("A1" & x).Select 

End Sub 

1 个答案:

答案 0 :(得分:0)

然后可能是这样的:

Sub Run()
    Dim rangeFrom As Range, wsTo As Worksheet
    Set rangeFrom = ThisWorkbook.Sheets("Table").UsedRange
    Set wsTo = Sheets("HAA")
    wsTo.Columns("A").Insert xlToRight, xlFormatFromLeftOrAbove ' insert the column before the paste?

    rangeFrom.AutoFilter
    rangeFrom.AutoFilter 12, "associated"
    rangeFrom.Columns("A").Copy

    wsTo.Range("A1").PasteSpecial xlPasteValues
    rangeFrom.AutoFilter
End Sub

在最后一行之后粘贴:

Dim rangeTo As Range
Set rangeTo = wsTo.UsedRange ' or = wsTo.Range("A1").CurrentRegion
Set rangeTo = rangeTo.Offset(rangeTo.Rows.Count)(1) ' (1) is to get the first cell 
Application.Goto rangeTo ' optional if you want to see where the values will be pasted
rangeTo.PasteSpecial xlPasteValues