gpt4 book ai didi

VBA - 从另一个工作簿中删除一行?

转载 作者:行者123 更新时间:2023-12-04 20:47:26 25 4
gpt4 key购买 nike

昨天我在以下方面寻求帮助:

  • 从 VBA 宏创建新工作簿,工作簿“B”
  • 将一行数据从工作簿“A”复制到工作簿“B”
  • 在关闭之前以适当的格式保存工作簿“B”

  • 昨天的帖子 ( VBA - How to copy row in Excel from one workbook to another?) 为我的问题提供了可靠的解决方案。

    从那以后,我在代码方面取得了进一步的进展,并对它有了新的要求。我现在需要一段代码,根据工作簿'A' sheet1 的单元格 Ai 中的值是否与工作簿'B' sheet1 的单元格 Ai 中的值匹配(我是递增的整数变量)。这段代码在行中复制的代码部分之后运行很多,并且主行已经更新了新信息,因此这看起来有点落后。

    我在下面的内容应该让您对我拥有的内容有所了解:
    ...
    Do Until IsEmpty(newBk.Sheets("Bad Records").Range("A" & extFileRowCount))
    If newBk.Sheets("Bad Records").Range("A" & extFileRowCount) = _
    mainBk.Range("W" & sdRow).Value Then
    'then delete the row and reinsert
    '***** Stuck here. How do I remove the row? *****'
    newBk.Sheets("Bad Records").Range _
    ("A" & extFileRowCount).remove '???
    newRowCount = newRowCount + 1
    mainBk.Rows(sdRow).Copy newBk.Sheets _
    ("Bad Records").Rows(newRowCount)
    Else
    extFileRowCount = extFileRowCount + 1
    Loop
    ...

    我认为我的算法是正确的,但我坚持如何删除该行。任何帮助将不胜感激。

    资历架构

    最佳答案

    我不太清楚为什么你在循环中有这条线

    mainBk.Rows(sdRow).Copy newBk.Sheets _                
    ("Bad Records").Rows(newRowCount)

    但是要删除行,您可以使用此代码( UNTESTED )
    Sub Sample()
    Dim i As Long
    Dim Delrange As Range

    Application.ScreenUpdating = False

    '
    '~~> Rest of the code
    '

    For i = extFileRowCount To 1 Step -1
    If newBk.Sheets("Bad Records").Range("A" & extFileRowCount) = _
    mainBk.Range("W" & sdRow).Value Then

    If Delrange Is Nothing Then
    Set Delrange = newBk.Sheets("Bad Records").Rows(i)
    Else
    Set Delrange = Union(Delrange, newBk.Sheets("Bad Records").Rows(i))
    End If
    End If
    Next i

    If Not Delrange Is Nothing Then Delrange.Delete

    '
    '~~> Rest of the code
    '

    Application.ScreenUpdating = True
    End Sub

    高温高压

    关于VBA - 从另一个工作簿中删除一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10244655/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com