如何从列中选择满足多个条件的行(不匹配)?

时间:2015-11-12 04:02:56

标签: excel vba excel-vba

我正在尝试使用Excel中的VBA编写宏来将基于列A的行复制到新的工作表中。例如,在下表中,仅将列为A的3,4,5和6行复制到新工作表中。任何帮助是极大的赞赏。

下表如下所示。

Column A ColumnB
1        1 
1        2 
1        7 
2        -
2        -
3        -
3        -
4        -
5        -
5        -
6        -
7        -

2 个答案:

答案 0 :(得分:0)

Sub sorter()
Dim find1 As Object, find2 As Object
Dim row1 As Integer, row2 As Integer
Dim myWB As Workbook

Set myWB = ThisWorkbook
row1 = 1
row2 = 1

Do
    Set find1 = myWB.Sheets(1).Range("B:B").Find(What:=myWB.Sheets(1).Cells(row1, 1).Value, LookIn:=xlValues)
    If find1 Is Nothing Then
        Set find2 = myWB.Sheets(2).Range("A:A").Find(What:=myWB.Sheets(1).Cells(row1, 1).Value, LookIn:=xlValues)
        If find2 Is Nothing Then
            myWB.Sheets(2).Cells(row2, 1).Value = myWB.Sheets(1).Cells(row1, 1).Value
            row2 = row2 + 1
        End If
    End If
row1 = row1 + 1
Loop Until myWB.Sheets(1).Cells(row1, 1).Value = ""

End Sub

请确保您不要在第一栏中填空:)

答案 1 :(得分:0)

@Lance,请您确认这是否是正确的答案?我修改了你的代码以满足这些要求:1)我想要列A中的所有行(不是唯一的)没有出现在列B中; 2)我需要将整行复制到新工作表; 3)我想在开头定义工作表名称。谢谢!

此外,我在这里遇到电脑电源问题。我的列A有62,346行,我的列B有7,167行,总共有30列。我尝试了我的代码,它从未成功运行:( 我使用的是惠普ProBook 440G2笔记本电脑英特尔酷睿i5-5200U CPU @ 2.2GHz,8 GB RAM,64位操作系统。我被要求更新我的笔记本电脑,但我不知道应该要求多少(就CPU和RAM而言)。在某些预算下,我是否要求更多CPU?内存? ??或者我不应该使用excel做这项工作?有什么建议?非常感谢。

Sub sorter()
Dim find1 As Object
', find2 As Object
Dim row1 As Long, row2 As Long
'Dim myWB As Workbook

'Set myWB = ThisWorkbook
row1 = 2
row2 = 2

Set t = Sheets("Sheet1")
Set r = Sheets("Sheet2")


Do
    Set find1 = t.Range("B:B").Find(What:=t.Cells(row1, 1).Value, LookIn:=xlValues)
    If find1 Is Nothing Then
        'Set find2 = myWB.Sheets(2).Range("A:A").Find(What:=myWB.Sheets(1).Cells(row1, 1).Value, LookIn:=xlValues)
        'If find2 Is Nothing Then
        r.Rows(row2).Value = t.Rows(row1).Value
        row2 = row2 + 1
    'End If
    End If
    row1 = row1 + 1
    Loop Until t.Cells(row1, 1).Value = ""

    End Sub
相关问题