2016-11-09 15 views
0

我需要查看每行上的两个单元格(C和F),并且如果C的值以30结尾,并且F的值大于零,将该行粘贴到另一张纸上。我已经设法使用1个标准来获得复制和粘贴工作,但我无法弄清楚如何让两个标准一起工作。VBA如果在两列中有两个标准

Sub compile1() 
    Dim x As String 

Set rSearch = Sheets("Application").Range("C:C") 


For Each cell In rSearch 
x = cell.Value 
     If Right(cell, 2) = "30" And cell.Offset(, 3) > 0 Then 

     matchRow = cell.Row 
     Rows(matchRow & ":" & matchRow).Select 
     Selection.Copy 

     Sheets("sheet2").Select 
     ActiveSheet.Rows(matchRow).Select 
     ActiveSheet.Paste 
     Sheets("Application").Select 
    End If 

Next 

End Sub 
+0

你说你的问题的答案, ,但我会使用你的评论线,'如果右(x,2)=“30”和x.offset(0,3).value> 0然后' –

+0

你的范围是错误的,你只需要C in在那里,偏移量移动到F,并使用X而不是单元格作为值比较=“30” –

+0

@Nathan_Sav谢谢。固定和现在工作! –

回答

1

在这里你去:

Sub CP() 

Dim i As Long 
Dim n As Long 

n = Sheets("Application").Cells(Rows.Count, 3).End(xlUp).Row 

For i = 1 To n 
    With Sheets("Application") 
     If Right(Cells(i, 3), 2) = 30 And Cells(i, 6).Value > 0 Then 
      .Cells(i, 3).EntireRow.Copy Destination:=Sheets("Sheet3").Cells(i, 3) 
      .Cells(i, 6).EntireRow.Copy Destination:=Sheets("Sheet3").Cells(i, 6) 
     End If 
    End With 
Next i 

End Sub 

我已经使用3列数行数,因此认为这是主要的列

+0

数据从Col A开始并结束Col L,如果满足条件,我需要复制整个行,而不仅仅是两个单元格。 –

0

你失踪在你的第二个为each loopNext声明。 这两个指标分析可与该行采取:

If y > 0 And Right(x, 2) = "30" Then 

所以整个代码将是...

Sub compile1() 
Dim x As String 
Dim y As Integer 
Dim rSearch As Range 
Dim rSearch1 As Range 
Dim cell As Range, cell1 As Range 
Dim matchRow As Integer 

Set rSearch = Sheets("Application").Range("C:c") 
Set rSearch1 = Sheets("Application").Range("F:F") 

For Each cell In rSearch 
    x = cell.Value 
    For Each cell1 In rSearch1 
    y = cell1.Value 
     If y > 0 And Right(x, 2) = "30" Then 
      matchRow = cell.Row 
      Rows(matchRow & ":" & matchRow).Select 
      Selection.Copy 

      Sheets("sheet2").Select 
      ActiveSheet.Rows(matchRow).Select 
      ActiveSheet.Paste 
      Sheets("Application").Select 
     End If 
    Next cell1 
Next cell 

End Sub 
0

为了加快速度,我建议如下:

Sub Copy_Paste() 
Dim x As String 
Dim y As Integer 
Dim WS1 As Worksheet 

Set WS1 = ActiveSheet 
y = 1 
Do Until y > WorksheetFunction.Max(Range("C1048576").End(xlUp).Row, Range("F1048576").End(xlUp).Row) 
    x = Trim(Cells(y, 3).Value) 
    If Right(x, 2) = "30" And (IsNumeric(Cells(y, 6).Value) And Cells(y, 6).Value > 0) Then Rows(y & ":" & y).Copy: Sheets("Sheet2").Range("A" & Sheets("Sheet2").Range("C1048576").End(xlUp).Row + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False: Application.CutCopyMode = False 
    y = y + 1 
Loop 

Sheets("Sheet2").Activate 
Range("A1").Activate 
WS1.Activate 

End Sub 
+1

进一步的性能增强将是使用数组,所以'arr1 = range(c1:c100).value','arr1 = range(f1:f100).value',然后循环数组 –

+0

@Nathan_Sav虽然数组通常是改善性能的好方法,我不认为在这里是这种情况(高兴地被纠正虽然) – Jeremy

+0

阵列16ms,在我的7000行测试范围31ms :) –

0

试试这个代码一次 - 这是太简单和优化处理比循环(更慢)

Application.ScreenUpdating = False 
Application.EnableEvents = False 

Sheets("Application").AutoFilterMode = False 

Dim lastrow, lastcol As Integer 
lastrow = Range("F500000").End(xlUp).Row 
lastcol = Sheets("Application").Range("A1").End(xlToRight).Column + 1 

Sheets("Application").Cells(1, lastcol).Value = "helper" 
Sheets("Application").Range(Sheets("Application").Cells(1, lastcol),Sheets("Application").Cells(lastrow, lastcol)).FormulaR1C1 = "=Right(RC[-1],2)" 

Sheets("Application").Range(Range("A1"), Range("A1").End(xlToRight)).AutoFilter Field:=lastcol, Criteria1:="30" 
Sheets("Application").Range(Range("A1"), Range("A1").End(xlToRight)).AutoFilter Field:=3, Criteria1:=">0" 

Sheets("Application").Range(Cells(1, 1), Cells(lastrow, lastcol)).SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("A2") 

Columns(lastcol).Delete 

Application.ScreenUpdating = True 
Application.EnableEvents = True 
+0

如果我将x = cell.Value If Right(x,2)="30"Then ForEach cell1 In rSearch1y = cell1.Value If y >0Then替换为If Right(cell, 2) = "30" And cell.Offset(, 3) > 0 Then,那么它正确地省略了col F中的值为负数的行,但它正在拉动Col F中最后两位数碰巧是30.我需要的只是Col C的最后两位数字是30,Col大于0. –

+0

您是否运行我的代码,这正是您所需要的。 –

+0

它所做的只是在Col C中添加了一个没有选中的过滤器。 (“Application”)。Range(Cells(1,1),Cells(lastrow,lastcol))。SpecialCells(xlCellTypeVisible).Copy Destination: –

0
Sub compile1() 
Dim Cel As Range, Rng As Range 

Set rSearch = Sheets("Application").Columns("C:C").SpecialCells(xlCellTypeConstants, 23) 

For Each Cel In rSearch 
    If Right(Trim(Cel.Value), 2) = "30" And (Cel.Offset(, 3).Value > 0) Then 
     Cel.EntireRow.Copy 
     Sheets("Sheet2").Range("A" & Sheets("Sheet2").Range("C1048576").End(xlUp).Row + 1).Paste 
     Application.CutCopyMode = False 
    End If 
Next 

End Sub 
+0

这需要一段时间,因为它会检查工作簿中的每一行。 – Jeremy

+0

您应该避免使用'。不惜一切代价选择'指令,因为它速度较慢,发生错误的可能性较高。你应该也得到最后一行的信息,因为循环所有的行,直到最后不喜欢非常有效... – RCaetano

+0

@Jeremy,你有什么其他建议,我怎么能让它更快?当我需要它的时候,我已经设法让整个代码工作并粘贴,但是你是对的,它需要和年龄来运行。 –

0

这是整个代码。它的工作原理但需要很长时间才能运行任何帮助,以加快它将不胜感激。

Sub Master() 
Call compile1 
Call compile2 
End Sub 
Sub compile1() 
For Each cell In Sheets("Application").Range("C:C") 
    If Right(cell.Value, 2) = "10" Then 
     matchRow = cell.Row 
     Rows(matchRow & ":" & matchRow).Select 
     Selection.Copy 

     Sheets("Routine w credits").Select 
     ActiveSheet.Rows(matchRow).Select 
     ActiveSheet.Paste 
     Sheets("Application").Select 
    End If 
Next 

For Each cell In Sheets("Application").Range("C:C") 
    If Right(cell.Value, 2) = "20" Then 
     matchRow = cell.Row 
     Rows(matchRow & ":" & matchRow).Select 
     Selection.Copy 

     Sheets("Reactive w credits").Select 
     ActiveSheet.Rows(matchRow).Select 
     ActiveSheet.Paste 
     Sheets("Application").Select 
    End If 
Next 

End Sub 

Sub compile2() 

Set rSearch = Sheets("Application").Range("C:C") 

For Each cell In rSearch 

    If Right(cell, 2) = "20" And cell.Offset(, 3) > 0 Then 

     matchRow = cell.Row 
     Rows(matchRow & ":" & matchRow).Select 
     Selection.Copy 

     Sheets("Reactive wo credits").Select 
     ActiveSheet.Rows(matchRow).Select 
     ActiveSheet.Paste 
     Sheets("Application").Select 
    End If 

Next 

For Each cell In rSearch 

    If Right(cell, 2) = "10" And cell.Offset(, 3) > 0 Then 

     matchRow = cell.Row 
     Rows(matchRow & ":" & matchRow).Select 
     Selection.Copy 

     Sheets("Routine wo credits").Select 
     ActiveSheet.Rows(matchRow).Select 
     ActiveSheet.Paste 
     Sheets("Application").Select 
    End If 

Next 
End Sub