2012-09-11 99 views
7

我一直在环顾四周如何在保存文档之前调用我的宏。我发现了这一点,但是当我把代码放入它的时候,它什么都不做。所以我想我错过了一些东西。Excel VBA - 在保存之前运行宏

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 
+1

你把这个代码'ThisWorkbook'模块中? –

+0

在什么?对不起,这是全新的。 – CustomX

+0

什么不起作用?保存之前你想做什么? – Passerby

回答

20

将您的代码ThisWorkbook模块

Place your code here

+0

当您处于设计模式时,您的Workbook_BeforeSave事件也可能不会执行,因此请确保在保存工作簿之前退出设计模式。 –