使用搜索条件从范围内的单元格复制所有行

时间:2015-01-18 15:16:37

标签: excel vba excel-vba

我搜索vba脚本,搜索单元格范围内的值,并将包含look值的所有行复制到另一个工作表,即:

搜索表:

column_1 column_2 column_3 column_4 column_5
  1      value_a  value_b  value_c  value_d
  2      value_e  value_a  value_f  value_g
  3      value_h  value_i  value_j  value_k
  4      value_l  value_a  value_m  value_n

在单元格“Z1”中查找值(例如value_a)并且我想在上面的表中搜索它,而不是复制(到新工作表)包含该值的所有行,即:

新报道:

  1      value_a  value_b  value_c  value_d
  2      value_e  value_a  value_f  value_g
  4      value_l  value_a  value_m  value_n

1 个答案:

答案 0 :(得分:0)

试试这个:

Sub copyrow()
col = 1
actrow = 2 'start in row 2

While Not IsEmpty(Cells(actrow, col)) 'check A2:A last row

Do While Not IsEmpty(Cells(actrow, col))
If Cells(actrow, col) = Cells(1, 24) Then 'check actual cell with Z1
Range("A" & actrow, "D" & actrow).Select 'select range to copy
Selection.copy
Worksheets("new").Activate 'new = sheet to copy the data
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastrow = lastrow + 1
Cells(lastrow, 1).Select
ActiveSheet.Paste 'copy in next free row
Worksheets("Tabelle1").Activate 'table1 = your sheet with data
col = col + 1
Exit Do

Else
col = col + 1
End If
Loop

actrow = actrow + 1
col = 1
Wend

End Sub