Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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/3/clojure/3.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
如何使用Excel VBA比较两个工作表并保留具有唯一ID的行_Vba_Excel - Fatal编程技术网

如何使用Excel VBA比较两个工作表并保留具有唯一ID的行

如何使用Excel VBA比较两个工作表并保留具有唯一ID的行,vba,excel,Vba,Excel,基本上,我尝试使用Excel和VBA查询工作表中的新值,对查询数据运行一些检查和计算,并归档满足特定条件的行。我将当前工作表与存档工作表交叉引用,并将存档工作表中尚未存在的任何行复制到其中。我使用了两个for循环来迭代每个工作表中的id,但正如我和前面的每个人一样,一旦行号变得重要,宏就会变得非常慢。我曾尝试使用带有变量的范围示例,但只能在列之间进行比较,以返回列值,而不是整行 我的循环代码示例如下所示: Dim ALastRow As Long, ALastCol As Long With A

基本上,我尝试使用Excel和VBA查询工作表中的新值,对查询数据运行一些检查和计算,并归档满足特定条件的行。我将当前工作表与存档工作表交叉引用,并将存档工作表中尚未存在的任何行复制到其中。我使用了两个for循环来迭代每个工作表中的id,但正如我和前面的每个人一样,一旦行号变得重要,宏就会变得非常慢。我曾尝试使用带有变量的范围示例,但只能在列之间进行比较,以返回列值,而不是整行

我的循环代码示例如下所示:

Dim ALastRow As Long, ALastCol As Long
With ActiveSheet
    ALastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    ALastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With

Dim iRow2 As Integer, counter As Integer, IDrow As Integer, match As Integer
Dim This_Sheet4 As Worksheet

ActiveWorkbook.Sheets(QueryArray(i, 1)).Activate
Set This_Sheet4 = ThisWorkbook.Sheets(Archive)

counter = 1
IDrow = 5
For iRow = 2 To LastRow2
    match = 0
    For iRow2 = 2 To ALastRow
        If match = 0 Then
            If iRow2 = ALastRow Then
                    If ThisWorkbook.Sheets(QueryArray(i, 1)).Cells(iRow, 12).Value > 1.11111111111111E-02 Then
                            If ThisWorkbook.Sheets(QueryArray(i, 1)).Cells(iRow, IDrow).Value = ThisWorkbook.Sheets(Archive).Cells(iRow2, IDrow).Value Then
                            match = 1
                            Else
                            ThisWorkbook.Sheets(QueryArray(i, 1)).Rows(iRow).Copy Destination:=ThisWorkbook.Sheets(Archive).Rows(ALastRow + counter)
                            counter = counter + 1
                            ActiveWorkbook.Sheets(Archive).Activate
                            Cells(iRow2, 1).Select
                            End If
                        End If
                Else
                    If ThisWorkbook.Sheets(QueryArray(i, 1)).Cells(iRow, IDrow).Value = ThisWorkbook.Sheets(Archive).Cells(iRow2, IDrow).Value Then
                    match = 1
                    Else
                    ActiveWorkbook.Sheets(QueryArray(i, 1)).Activate
                    Cells(iRow, 1).Select
                    End If
                End If
            Else
            End If
        Next iRow2
    Next iRow
Next

如果您能够将列添加到当前工作表数据中(我将其隐藏,这样就不会有人对其进行更改!),那么您只需让宏在存档行后在“已存档”列中填充一个值——“x”、时间戳等。然后,当您运行宏以归档满足相应条件的列时,您可以

1) 将表筛选为Criteria1:=vbNullString以仅获取未归档的行并使用myRange.SpecialCells(xlCellTypeVisible)

例如:

Dim currentSheet as Worksheet
Dim TblLength as long
Dim Table as string

Table = "TableName"
Set currentSheet = ActiveSheet

With currentSheet.Range(Table)
   TblLength = .Columns(1).Rows.Count
   'clear any existing filters
   .AutoFilter  

   'set new filters
   .AutoFilter Field:=currentSheet.Range(Table & "[Transferred]").Column, Criteria1:=vbNullString
End With

If currentSheet.Range("A1:A" & TblLength + 1).Columns(1).SpecialCells(xlCellTypeVisible).Count - 1 > 0 Then
  'make sure we have visible rows
  Set CopyRange = currentSheet.SpecialCells(xlCellTypeVisible).Copy
  'other code
End If
For Row = 1 To Range(Table).Rows.Count
   If Range(Table & "[Archived]")(Row) = vbNullString Then
          'your code here
   End if
Next Row
archiveSheet.Sort Key1:="Cusip", order1:=xlAscending, header:=xlYes

For iRow2 = 2 To ALastRow
    match = 0
    'assuming the cusip is in column A...
    myCusip = currentSheet.Range("A" & iRow2).Value
    'we want to know if we've reached it yet
    cusipMatched = false
    For iRow = 2 To LastRow2

        If myCusip = archiveSheet.Range("A" & iRow).Value Then
           cusipMatched = true
           'your code to check if the row matches
        Else
           If cusipMatched Then
              'we've already reached our cusip, so now we are past it
              ' this exits the current For loop and continues on in the outer For loop
              Exit For
           End if
        End if
   Next iRow
Next iRow2

2) 循环检查当前工作表中的所有行,并测试范围(“表[已存档]”)(行)=vbNullString,以确定是否已存档

例如:

Dim currentSheet as Worksheet
Dim TblLength as long
Dim Table as string

Table = "TableName"
Set currentSheet = ActiveSheet

With currentSheet.Range(Table)
   TblLength = .Columns(1).Rows.Count
   'clear any existing filters
   .AutoFilter  

   'set new filters
   .AutoFilter Field:=currentSheet.Range(Table & "[Transferred]").Column, Criteria1:=vbNullString
End With

If currentSheet.Range("A1:A" & TblLength + 1).Columns(1).SpecialCells(xlCellTypeVisible).Count - 1 > 0 Then
  'make sure we have visible rows
  Set CopyRange = currentSheet.SpecialCells(xlCellTypeVisible).Copy
  'other code
End If
For Row = 1 To Range(Table).Rows.Count
   If Range(Table & "[Archived]")(Row) = vbNullString Then
          'your code here
   End if
Next Row
archiveSheet.Sort Key1:="Cusip", order1:=xlAscending, header:=xlYes

For iRow2 = 2 To ALastRow
    match = 0
    'assuming the cusip is in column A...
    myCusip = currentSheet.Range("A" & iRow2).Value
    'we want to know if we've reached it yet
    cusipMatched = false
    For iRow = 2 To LastRow2

        If myCusip = archiveSheet.Range("A" & iRow).Value Then
           cusipMatched = true
           'your code to check if the row matches
        Else
           If cusipMatched Then
              'we've already reached our cusip, so now we are past it
              ' this exits the current For loop and continues on in the outer For loop
              Exit For
           End if
        End if
   Next iRow
Next iRow2
或者,您可以按其中一列对归档表进行排序—例如,如果列出证券销售和购买,则按Cusip列进行排序。然后,您可以通过currentSheet进行第一个循环,并通过archiveSheet测试对内部循环进行第一次检查,以查看CUSIP是否匹配。这样,一旦您通过archiveSheet中的cusip,就可以退出循环-仍然需要大量的循环,但只需要少一点

例如:

Dim currentSheet as Worksheet
Dim TblLength as long
Dim Table as string

Table = "TableName"
Set currentSheet = ActiveSheet

With currentSheet.Range(Table)
   TblLength = .Columns(1).Rows.Count
   'clear any existing filters
   .AutoFilter  

   'set new filters
   .AutoFilter Field:=currentSheet.Range(Table & "[Transferred]").Column, Criteria1:=vbNullString
End With

If currentSheet.Range("A1:A" & TblLength + 1).Columns(1).SpecialCells(xlCellTypeVisible).Count - 1 > 0 Then
  'make sure we have visible rows
  Set CopyRange = currentSheet.SpecialCells(xlCellTypeVisible).Copy
  'other code
End If
For Row = 1 To Range(Table).Rows.Count
   If Range(Table & "[Archived]")(Row) = vbNullString Then
          'your code here
   End if
Next Row
archiveSheet.Sort Key1:="Cusip", order1:=xlAscending, header:=xlYes

For iRow2 = 2 To ALastRow
    match = 0
    'assuming the cusip is in column A...
    myCusip = currentSheet.Range("A" & iRow2).Value
    'we want to know if we've reached it yet
    cusipMatched = false
    For iRow = 2 To LastRow2

        If myCusip = archiveSheet.Range("A" & iRow).Value Then
           cusipMatched = true
           'your code to check if the row matches
        Else
           If cusipMatched Then
              'we've already reached our cusip, so now we are past it
              ' this exits the current For loop and continues on in the outer For loop
              Exit For
           End if
        End if
   Next iRow
Next iRow2

这与mysql无关。请删除该标签。您是否尝试过将一个标签叠在另一个标签上并使用删除重复标签?@RashminJaviya关于您对我建议的编辑所做的更改,请参阅关于删除帖子中的问候语和类似内容。@RashminJaviya:“提前感谢”是应该从问题中删除的绒毛,您不应该编辑编辑建议。。。