查找下一行有时会覆盖现有数据

find next line sometimes overwrites existing data

我希望你能帮助我将下面的代码作为一个更大的宏的一部分,但不是将值粘贴到底部的下一个可用行中,而是有时会覆盖现有数据。

    Application.ScreenUpdating = False 
    Dim copySheet As Worksheet
    Dim pasteSheet As Worksheet
    Set copySheet = Worksheets("Helper")
    Set pasteSheet = Worksheets("Purchases 2021")
    copySheet.Range("A2:K2").Copy
    pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
    Application.CutCopyMode = False

作品sheet用于生成采购订单并记录购买情况,单击按钮时,宏会生成采购订单的电子邮件,将详细信息粘贴到跟踪中的下一行 A-K 列sheet 其中 L-AK 列中有一些公式和其他手动输入的数据,财务团队使用这些数据来跟踪收到的货物和开具发票。 它似乎只发生在用户一次输入多个采购订单的情况下,但我无法复制错误,所以我无法确定它在哪里跌倒了。 sheet 被锁定,所以这不是偶然的,它一定是宏中的东西。

编辑以添加整个代码(我已删除此处的密码):

'Private Sub CommandButton1_Click()
Sheets("Purchase order form").Unprotect Password:=
Sheets("Helper").Unprotect Password:=
Sheets("Helper2").Unprotect Password:=
Sheets("recurring orders 2021").Unprotect Password:=
Sheets("Purchases 2021").Unprotect Password:=
Range("M7").Copy
Range("M7").PasteSpecial Paste:=xlPasteValues

Calculate

Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object

Set rng = Nothing
Set rng = Sheet1.Range("A1:N43").SpecialCells(xlCellTypeVisible)
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected. " & _
       vbNewLine & "Please correct and try again.", vbOKOnly
Exit Sub
End If

With Application
.EnableEvents = False
.ScreenUpdating = False
End With

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With OutMail
.To = Sheet1.Range("H12")
.CC = "xxx@xxx.xxx"
.BCC = ""
.Subject = Sheet1.Range("M12")
.HTMLBody = RangetoHTML(rng)
.Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With

Set OutMail = Nothing
Set OutApp = Nothing

Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = Worksheets("Helper")
Set pasteSheet = Worksheets("Purchases 2021")
copySheet.Range("A2:L2").Copy
pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False

Sheets("Purchase order form").Protect Password:=
Sheets("Helper").Protect Password:=
Sheets("Helper2").Protect Password:=
Sheets("recurring orders 2021").Protect Password:=, AllowFiltering:=True
Sheets("Purchases 2021").Protect Password:=, AllowFiltering:=True
ActiveWorkbook.Save
End Sub

Function RangetoHTML(rng As Range)
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook

TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
    .Cells(1).PasteSpecial Paste:=8
    .Cells(1).PasteSpecial xlPasteValues, , False, False
    .Cells(1).PasteSpecial xlPasteFormats, , False, False
    .Cells(1).Select
    Application.CutCopyMode = False
    On Error Resume Next
    .DrawingObjects.Visible = True
    .DrawingObjects.Delete
    On Error GoTo 0
End With
With TempWB.PublishObjects.Add( _
     SourceType:=xlSourceRange, _
     Filename:=TempFile, _
     Sheet:=TempWB.Sheets(1).Name, _
     Source:=TempWB.Sheets(1).UsedRange.Address, _
     HtmlType:=xlHtmlStatic)
    .Publish (True)
End With
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                      "align=left x:publishsource=")

TempWB.Close savechanges:=False

Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function

编辑我了解到有时保存工作簿时粘贴 sheet 已过滤 - 这会导致问题吗?

如果找不到根本原因,您可以进行防御性编程以警告用户并避免覆盖现有数据。

    Dim copySheet As Worksheet, pasteSheet As Worksheet
    Dim iRow As Long, rng As Range
    
    With ThisWorkbook
        Set copySheet = .Sheets("Helper")
        Set pasteSheet = .Sheets("Purchases 2021")
    End With
        
    With pasteSheet
        iRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
        Set rng = .Range("A" & iRow).Resize(1, 12) ' A-L

        ' ensure range is empty
        While WorksheetFunction.CountA(rng) > 0
            MsgBox rng.Address & " not blank, trying next one", vbExclamation
            Set rng = rng.Offset(1, 0)
        Wend
       
        rng = copySheet.Range("A2:L2").Value
        MsgBox "inserted into row " & rng.Row, vbInformation
    End With
  

感谢 @cdp1802 的帮助,我发现了问题,有一个隐藏的单元格对于某些条目是空白的,这些单元格是空白的,然后宏假设该行是空白的并放入新数据在那一行。 再次感谢您的建议。