VBA:根据if语句/循环复制并粘贴整行,并推送到3张新表

时间:2017-05-30 01:43:22

标签: vba excel-vba excel

我是VBA编程的新手,并且在理解如何将这些概念放在一起时遇到了非常困难的时间。

我的目标是根据条件选择多行(每列中有多个字符串),例如“如果列h中的值小于50%,请复制具有此类条件的整行并移至新工作表称为'小于50'。如果列h中的值超过70%,则复制整行并移至名为“超过70%”的新工作表。如果列h中的值介于50-70%之间,请移至介于两者之间的一张纸。“

我已经尝试了几周查看教程和其他信息,但是无法理解。

这些都不起作用,但这几乎是我在过去几周里学到的东西,也是我试图使用我的代码的地方。

Sub cool() 
    i As Integer 
    For i = 2 To Number 
        If Cells(i, 3).Value < 50 Then 
            activecells.Rows().EntireRow.Copy 
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = under50
            Worksheets("under50").Range("A" & Rows.Count).End(xlUp).Offset(1).Select
        End If
    Next i
End Sub

1 个答案:

答案 0 :(得分:0)

这是对代码的重大改写,希望能够做到你想要的。我已经包含了一些解释我所做更改的注释,但是一旦我进入If语句,它就会与原始代码的区别在于继续添加许多注释。

Sub cool()
    'Need "Dim"
    'Recommend "Long" rather than "Integer" for referring to rows and columns
    'i As Integer
    Dim i As Long
    'Declare "Number"
    Dim Number As Long
    'Declare a variable to refer to the sheet you are going to copy from
    Dim wsSrc As Worksheet
    Set wsSrc = ActiveSheet
    'Declare a variable to refer to the sheet you are going to copy to
    Dim wsDest As Worksheet
    'Declare three other worksheet variables for the three potential destinations
    Dim wsLessThan50 As Worksheet
    Dim wsInBetween As Worksheet
    Dim wsOver70 As Worksheet
    'Create the three sheets - do this once rather than in the loop
    Set wsLessThan50 = Worksheets.Add(After:=Worksheets(Worksheets.Count))
    Set wsInBetween = Worksheets.Add(After:=Worksheets(Worksheets.Count))
    Set wsOver70 = Worksheets.Add(After:=Worksheets(Worksheets.Count))
    'Assign the worksheet names
    wsLessThan50.Name = "less than 50"
    wsInBetween.Name = "in between"
    wsOver70.Name = "over 70"

    'Determine last row in source sheet
    Number = wsSrc.Cells(wsSrc.Rows.Count, "C").End(xlUp).Row

    For i = 2 To Number
        'Always qualify "Cells" to specify which sheet you are referring to
        '(It defaults to ActiveSheet, but leaving it out when ActiveSheet is
        ' NOT intended to be used leads to about 25% of the problems on SO,
        ' so get into the habit of always specifying it.)
        'Question says column H, but you are referring to column C
        '50% is equal to 0.5, not 50
        'If Cells(i, 3).Value < 50 Then

        'Determine which destination sheet to use
        If wsSrc.Cells(i, "C").Value < 0.5 Then
            Set wsDest = wsLessThan50
        ElseIf wsSrc.Cells(i, "C").Value > 0.7 Then
            Set wsDest = wsOver70
        Else
            Set wsDest = wsInBetween
        End If

        'Copy the current row from the source sheet to the next available row on the
        'destination sheet
        With wsDest
            'This next line will start placing things in the destination sheets starting
            'from row 2.  I could say that that is intentional (so that you have a row
            'that you can use for headings) but it is really because I am too lazy to
            'include the extra lines to check whether row 1 is empty and, if so, copy to
            'row 1, else copy to the next available row.
            wsSrc.Rows(i).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
        End With
    Next i
End Sub