我希望您可以帮助我将下面的代码作为更大宏的一部分,但它不是将值粘贴到底部的下一个可用行中,而是有时会覆盖现有的数据。
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
工作表用于生成采购订单和记录采购记录,当单击按钮时,宏生成采购订单的电子邮件,将详细信息粘贴到跟踪表的下一行AK,其中财务小组使用track列中的一些公式和其他手动输入数据来跟踪收到的货物和开具发票。它似乎只发生在用户一次输入多个定购单时,但我无法复制错误,因此找出它掉在哪里有困难。工作表被锁定了,所以它不能是偶然的,它必须是宏中的东西。
编辑以添加整个代码(我在这里删除了密码):
'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
编辑我已经意识到,工作簿有时是保存与粘贴工作表过滤-这会导致问题吗?
发布于 2021-02-17 15:37:48
感谢help @ row 1802,我发现了这个问题,有些条目的隐藏单元格是空白的,在这些单元格为空的地方,宏假设行为空,并将新的数据放入该行。再次感谢你的建议。
发布于 2021-02-10 14:20:06
如果找不到根本原因,就可以进行防御性编程,警告用户并避免覆盖现有数据。
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
https://stackoverflow.com/questions/66136585
复制相似问题