Excel VBA - 如果行值复制其他工作表

时间:2016-02-27 11:58:31

标签: excel vba excel-vba

平,

我需要Excel的帮助。为了更好的不受欢迎,附上图片。 基本上我需要excel逐行遍历并使用设置值复制所有行(参见图片)。

  • 逻辑:
  • 如果col1值“1”复制(总是)'
  • 包含col3值“X”副本也在下面排(值= 2)
  • 如果col3不是“X”复制并跳到下一个col1 = 2
  • 如果col3不是“X”复制并跳到下一个col1 = 1
  • 如果col1值“1”复制(总是)
  • 包含col3值而不是“X”跳到下一个col1 = 1

修改:ATTACHED EXCEL FILE WITH EXAMPLE OUTPUT

Excel - Picture

If Sheets(1).Cells(i, 1).Value = 1 Then
    //*copy entire row and skip everything until Sheets(1).Cells(i, 1).Value = 1*//
    else if Sheets(1).Cells(i, 1).Value = 1 And Sheets(1).Cells(i, 3).Value = "x" Then
    *copy entire row and continue loop*

    If Sheets(1).Cells(i, 1).Value = 2 Then
    //*copy entire row and skip everything until Sheets(1).Cells(i, 1).Value = 2 or higer*//
    else if Sheets(1).Cells(i, 1).Value = 2 And Sheets(1).Cells(i, 3).Value = "x" Then
    *copy entire row and continue loop*

    If Sheets(1).Cells(i, 1).Value = 3 Then
    //*copy entire row and skip everything until Sheets(1).Cells(i, 1).Value = 3 or higher*//
    else if Sheets(1).Cells(i, 1).Value = 3 And Sheets(1).Cells(i, 3).Value = "x" Then
    *copy entire row and continue loop*


    If Sheets(1).Cells(i, 1).Value = 4 Then
    //*copy entire row and skip everything until Sheets(1).Cells(i, 1).Value = 4 or higher*//
    else if Sheets(1).Cells(i, 1).Value = 4 And Sheets(1).Cells(i, 3).Value = "x" Then
    *copy entire row and continue loop*

1 个答案:

答案 0 :(得分:0)

我已经对代码进行了更改,现在它从列A:E中复制数据,以便在单元格(i + 1,5)或单元格(counter_rows,5)中添加更多列更改为“5”并添加不同的列号。

带有数据的行存储在一个数组中,以加速代码并使其更紧凑。

此外,您应该将此子例程放在VBA中的MODULE中。不要向表格添加代码。

Sub copy_rows_to_sheet2()
Dim nb_rows As Integer
Dim counter_rows As Integer
Application.ScreenUpdating = False 'speed up code
nb_rows = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row  'count the number of rows with data on sheet(1)

counter_rows = 2 'the first row on sheet(2) where we start copying data from


Dim Arr() As Variant ' declare an unallocated array, stores data from range on the sheets (any number of rows and columns)

For i = 2 To nb_rows
      Sheets(1).Select  'to add data to array, first select the sheet1
        If Sheets(1).Cells(i, 1).Value = 1 Or Sheets(1).Cells(i, 1).Value = 2 Then
            If Sheets(1).Cells(i, 3).Value = "x" Then  'we copy 2 rows when we have x in col 3
             Arr = Range(Cells(i, 1), Cells(i + 1, 5)).Value  'copy all values from row i and next row counter_rows and columns (A to E=5)
             Sheets(2).Select  'before the array is pasted to sheet2 first it needs to be selected
             Range(Cells(counter_rows, 1), Cells(counter_rows + 1, 5)).Value = Arr
               counter_rows = counter_rows + 2 'counter increments by 2 rows
            Else

              Arr = Range(Cells(i, 1), Cells(i, 5)).Value  'copy row i and 5 columns
              Sheets(2).Select
              Range(Cells(counter_rows, 1), Cells(counter_rows, 5)).Value = Arr
                  counter_rows = counter_rows + 1 'counter increments by 1 row
            End If
        End If

Next i

Application.ScreenUpdating = False 'turn back

End Sub