Excel VBA - 保存前运行宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12365417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:59:33  来源:igfitidea点击:

Excel VBA - Run macro before save

excelexcel-vbavba

提问by CustomX

I've been looking around on how to call my macro just before I save my document. I found this, but when I put my code inside it, it doesn't do anything. So I presume I'm missing something.

在保存文档之前,我一直在寻找如何调用我的宏。我找到了这个,但是当我将代码放入其中时,它什么也没做。所以我想我错过了一些东西。

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

Set shtVO = Sheets("Voice orders")
endRowVO = shtVO.Range("E" & Rows.Count).End(xlUp).Row

For Row = 11 To endRowVO
    If IsEmpty(shtVO.Cells(Row, 23).Value) = False Then
        If shtVO.Cells(Row, 3).Value <> shtVO.Cells(Row, 23) Then
            If shtVO.Cells(Row, 1).Value Like "*MIG*" Then
            Else
                shtVO.Cells(Row, 1).Value = shtVO.Cells(Row, 1).Value + "MIG"
            End If
        End If
    End If
Next Row

End Sub 

回答by chris neilsen

Place your code in the ThisWorkbookmodule

将您的代码放在ThisWorkbook模块中

Place your code here

将您的代码放在这里