如何在保存事件中放置Excel

如何在保存事件中放置Excel

本文介绍了如何在保存事件中放置Excel VBA代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我保存Excel文档时,我想在VBA代码下运行.我可以立即运行此代码,但是(不在保存状态)它不再起作用.您能帮我如何在保存时运行此代码吗?

I want to run below VBA code when I save the excel document. I can able to run this code at once but (Not on save) it's not working anymore. Could you please help me how can I run this code on save?

它在模块中:

'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

推荐答案

不确定我是否正确理解了您的问题,但是 BeforeSave 事件在保存文件之前触发.

Not sure if I understood your question correctly, but the BeforeSave Event fires just before saving the file.

因此,您需要在ThisWorkbook模块中放置以下内容:

So you would need to put the following in ThisWorkbook module:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As   Boolean)

CopyToOtherCell

End sub

这篇关于如何在保存事件中放置Excel VBA代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:50