VBA中的循环编码

时间:2014-06-10 15:01:03

标签: vba loops copy-paste

假设我只想复制合同编号,在A栏中,其中包含" Y"在C列中(C列是标志,它是Y或N)并将数字粘贴到新标签中。我该怎么办?

我试着写些东西而且没能奏效。 =(

谢谢!

Sub Test()

Do

Count = 1

 On Error GoTo ErrHandler
 On Error Resume Next
If Range("CheckContract").Offset(Count, 2).Value = "Y" Then
Range("CheckContract").Offset(Count, 0).Copy
Range("PasteRange").Offset(Count, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
End If

Count = Count + 1

Loop While Range("CheckContract").Offset(Count, 0).Value <> ""
Exit Sub

ErrHandler:

MsgBox Prompt:="Please check the inputs", Buttons:=vbExclamation, Title:="Error Msg"
End Sub

1 个答案:

答案 0 :(得分:0)

这应该有效

Sub Macro1()

Dim lrow As Integer
lrow = ActiveSheet.UsedRange.Rows.Count
    ActiveSheet.Range("$A$1:$C$" & lrow).AutoFilter Field:=3, Criteria1:="Y"
    Columns("A:A").SpecialCells(xlCellTypeVisible).Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
End Sub