如何使用语法&会产生两次输出?

时间:2016-10-10 12:21:23

标签: excel vba excel-vba

Sub test()
    LastRow_Sheet1 = Worksheets("Sheet1").UsedRange.Rows.Count
    LastRow_Sheet2 = Worksheets("Sheet2").UsedRange.Rows.Count

For x = 2 To 30

    po_number = Worksheets("Sheet1").Cells(x, 1).Value
    site_name = Worksheets("Sheet1").Cells(x, 2).Value

    For y = 2 To LastRow_Sheet2
        If po_number <> Worksheets("Sheet1").Cells(y, 1).Value Then ' And (InStr(1, Cells(y, 30), CStr(site_name)) >= 1) Then
            With Worksheets("Sheet2")
            If InStr(1, .Cells(y, 30), CStr(site_name)) >= 1 Then
                .Range(.Cells(y, 1), .Cells(y, 31)).Copy
                nextRow = Sheets("Sheet3").Cells(Sheets("Sheet3").Rows.Count, 1).End(xlUp).Row + 1
                Sheets("Sheet3").Range("A" & nextRow).PasteSpecial
            End If
          End With
        End If
    Next
  Next
End Sub

大家好,

问题1,这两个if syntax会有相同的结果吗?

If po_number <> Worksheets("Sheet1").Cells(y, 1).Value And (InStr(1, Cells(y, 30), CStr(site_name)) >= 1) Then
            With Worksheets("Sheet2")
            .Range(.Cells(y, 1), .Cells(y, 31)).Copy
                nextRow = Sheets("Sheet3").Cells(Sheets("Sheet3").Rows.Count, 1).End(xlUp).Row + 1
                Sheets("Sheet3").Range("A" & nextRow).PasteSpecial
            End With
    End If

If po_number <> Worksheets("Sheet1").Cells(y, 1).Value Then
         With Worksheets("Sheet2")
            If InStr(1, .Cells(y, 30), CStr(site_name)) >= 1 Then
                .Range(.Cells(y, 1), .Cells(y, 31)).Copy
                nextRow = Sheets("Sheet3").Cells(Sheets("Sheet3").Rows.Count, 1).End(xlUp).Row + 1
                Sheets("Sheet3").Range("A" & nextRow).PasteSpecial
            End If
          End With
        End If

问题2,似乎我的原始代码会输出两次结果..任何人都可以帮我定义吗?

非常感谢..

0 个答案:

没有答案