我想在保存excel文档时运行VBA代码以下。我可以立即运行此代码,但(不保存)它不再工作了。你能帮我解决一下如何在保存时运行这段代码?
它在模块中:
'Option Explicit
'
'Sub CopyToOtherCell()
'
'Dim LastRow As Integer, i As Integer, erow As Integer
'
'LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
'
'For i = 2 To LastRow
' If Cells(i, 12).Value = Date Then
' Range(Cells(i, 1), Cells(i, 12)).Select
' Selection.Copy
' Workbooks.Open Filename:="C:\Users\Murat\Desktop\Proposal_Admin.xlsx"
' Worksheets("sheet1").Select
' erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
' ActiveSheet.Cells(erow, 1).Select
' ActiveSheet.Paste
' ActiveWorkbook.Save
' ActiveWorkbook.Close
' Application.CutCopyMode = False
' End If
'Next i
'
'End Sub
答案 0 :(得分:1)
不确定我是否正确理解了您的问题,但 BeforeSave 事件在保存文件之前就会触发。
因此,您需要在ThisWorkbook模块中添加以下内容:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
CopyToOtherCell
End sub