Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 比较2个excel表中的值,并在VBA中按降序排序_Sorting_Excel_Vba - Fatal编程技术网

Sorting 比较2个excel表中的值,并在VBA中按降序排序

Sorting 比较2个excel表中的值,并在VBA中按降序排序,sorting,excel,vba,Sorting,Excel,Vba,我想使用excel 2010实现一个函数,首先比较两个不同excel表中的值,然后根据另一列值对它们进行排序 例如: 在表1中,我得到: Name Value Test 1 100.5 Test 1 200.6 Test 1 300.3 Test 2 100.8 Test 2 200.6 Test 3 200.5 在表2中,我得到: Name Test 1 Test 1 Test 1 Test 3 我想要实现的是,如果工作表

我想使用excel 2010实现一个函数,首先比较两个不同excel表中的值,然后根据另一列值对它们进行排序

例如: 在表1中,我得到:

Name    Value
Test 1   100.5
Test 1   200.6
Test 1   300.3
Test 2   100.8
Test 2   200.6
Test 3   200.5
在表2中,我得到:

Name    
Test 1   
Test 1   
Test 1      
Test 3   
我想要实现的是,如果工作表1中的名称不在工作表2中,请删除工作表1中的整行,并根据列值降序排列名称

所需: 以下是我到目前为止得到的信息:

Sub test()
  Dim wb As Workbook
  Dim ws1 As Worksheet
  Dim ws2 As Worksheet

  Dim i As Integer, j As Integer
  Dim lastRow1 As Integer, lastRow2 As Integer


On Error GoTo 0

Set wb = ActiveWorkbook
Set ws1 = wb.Worksheets("Sheet1")
Set ws2 = wb.Worksheets("Sheet2")


lastRow1 = ws1.UsedRange.Rows.Count
lastRow2 = ws2.UsedRange.Rows.Count


For i = 2 To lastRow1
    For j = 2 To lastRow2
        If ws1.Cells(i, 1).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
            If InStr(1, ws2.Cells(j, 1).Value, ws1.Cells(i, 1).Value, vbTextCompare) < 1 Then 
                Rows(i).EntireRow.delete

              Exit For
           End If
        End If
    Next j
Next i
End Sub
子测试()
将wb设置为工作簿
将ws1设置为工作表
将ws2设置为工作表
尺寸i为整数,j为整数
将lastRow1设置为整数,将lastRow2设置为整数
错误转到0
设置wb=ActiveWorkbook
设置ws1=wb.工作表(“表1”)
设置ws2=wb.工作表(“表2”)
lastRow1=ws1.UsedRange.Rows.Count
lastRow2=ws2.UsedRange.Rows.Count
对于i=2到最后一行1
对于j=2到最后一行2
如果ws1.Cells(i,1).Value为“”,则这将忽略末尾的空白单元格(如果列长度不相等)。
如果InStr(1,ws2.Cells(j,1).Value,ws1.Cells(i,1).Value,vbTextCompare)<1,那么
行(i).EntireRow.delete
退出
如果结束
如果结束
下一个j
接下来我
端接头

请提供建议和帮助。非常感谢您。

仍在考虑其余答案,但我建议您先从列表底部开始(从最后一行到第二行)原因是您正在删除计数器未考虑的行。您可能还想查看Excel中的MATCH函数,查看列表中是否使用了某个值,而不是查看整个列表。

我更改了您的代码,使其正常工作:

Sub test()
  Dim wb As Workbook
  Dim ws1 As Worksheet
  Dim ws2 As Worksheet

  Dim i As Integer, j As Integer
  Dim lastRow1 As Integer, lastRow2 As Integer


On Error GoTo 0

Set wb = ActiveWorkbook
Set ws1 = wb.Worksheets("Sheet1")
Set ws2 = wb.Worksheets("Sheet2")

lastRow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row 'last used cell in column A
lastRow2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row 'last used cell in column A

Dim same As Boolean
same = False

For i = lastRow1 To 2 Step -1 'bottom to top
    For j = 2 To lastRow2
    Debug.Print ws1.Cells(i, 1).Value
    Debug.Print ws2.Cells(j, 1).Value
        If ws1.Cells(i, 1).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.

            If ws1.Cells(i, 1).Value = ws2.Cells(j, 1).Value Then
                same = True 'set True if match

           End If
        End If
    Next j

                If same = False Then 'if no match
                    Rows(i).EntireRow.Delete
                End If
                same = False
Next i

'sort
lastRow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
Range("A2:B" & lastRow1).Sort key1:=Range("A2:A" & lastRow1), order1:=xlAscending, Header:=xlNo, key2:=Range("B2:B" & lastRow1), order2:=xlAscending, Header:=xlNo

End Sub
子测试()
将wb设置为工作簿
将ws1设置为工作表
将ws2设置为工作表
尺寸i为整数,j为整数
将lastRow1设置为整数,将lastRow2设置为整数
错误转到0
设置wb=ActiveWorkbook
设置ws1=wb.工作表(“表1”)
设置ws2=wb.工作表(“表2”)
lastRow1=ws1.Cells(Rows.Count,1).End(xlUp).Row'列中最后使用的单元格
lastRow2=ws2.Cells(Rows.Count,1).End(xlUp).Row'列中最后使用的单元格
与布尔值相同
相同=错误
对于i=lastRow1至2步骤-1'底部至顶部
对于j=2到最后一行2
Debug.Print ws1.Cells(i,1).Value
Debug.Print ws2.Cells(j,1).Value
如果ws1.Cells(i,1).Value为“”,则这将忽略末尾的空白单元格(如果列长度不相等)。
如果ws1.Cells(i,1).Value=ws2.Cells(j,1).Value,则
相同=真“如果匹配则设置真
如果结束
如果结束
下一个j
如果相同=错误,则“如果不匹配”
行(i).EntireRow.Delete
如果结束
相同=错误
接下来我
”“好吧
lastRow1=ws1.Cells(Rows.Count,1).End(xlUp).Row
Range(“A2:B”和lastRow1)。排序键1:=Range(“A2:A”和lastRow1),顺序1:=xlAscending,标头:=xlNo,键2:=Range(“B2:B”和lastRow1),顺序2:=xlAscending,标头:=xlNo
端接头

能否更具体地说明从最后一行到第二行的删除功能?我已经使用了函数Instr()要比较不同工作表中的单元格值,如果在工作表2中找不到工作表1的名称,因此该位置返回0,但工作不正常,请给出建议,非常感谢。如果从列表顶部开始,在第5行找到一个必须删除的值,然后删除该行,则for下一个循环将转到6,但这是一个row位于您现在想要的行下方,即第5行,在删除第5行之前是第6行。我希望这一点很清楚,否则我将不得不尝试更好地解释它。对于第二部分:如果您仍然希望循环中的值以比较值,请使用StrComp函数而不是InStr。这不考虑任何影响虽然有额外的空格,但你可能也希望使用修剪功能来删除这些空格。这实际上是一个注释,而不是答案。非常感谢。这正是我想要的!
Sub test()
  Dim wb As Workbook
  Dim ws1 As Worksheet
  Dim ws2 As Worksheet

  Dim i As Integer, j As Integer
  Dim lastRow1 As Integer, lastRow2 As Integer


On Error GoTo 0

Set wb = ActiveWorkbook
Set ws1 = wb.Worksheets("Sheet1")
Set ws2 = wb.Worksheets("Sheet2")

lastRow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row 'last used cell in column A
lastRow2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row 'last used cell in column A

Dim same As Boolean
same = False

For i = lastRow1 To 2 Step -1 'bottom to top
    For j = 2 To lastRow2
    Debug.Print ws1.Cells(i, 1).Value
    Debug.Print ws2.Cells(j, 1).Value
        If ws1.Cells(i, 1).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.

            If ws1.Cells(i, 1).Value = ws2.Cells(j, 1).Value Then
                same = True 'set True if match

           End If
        End If
    Next j

                If same = False Then 'if no match
                    Rows(i).EntireRow.Delete
                End If
                same = False
Next i

'sort
lastRow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
Range("A2:B" & lastRow1).Sort key1:=Range("A2:A" & lastRow1), order1:=xlAscending, Header:=xlNo, key2:=Range("B2:B" & lastRow1), order2:=xlAscending, Header:=xlNo

End Sub