Tạo chức năng Undo cho Add-In

Liên hệ QC

killitmore

Thành viên hoạt động
Tham gia
8/12/14
Bài viết
165
Được thích
72
Em có mò được bài báo sau hướng dẫn cách Undo cho code VBA: http://www.jkp-ads.com/Articles/UndoWithVBA00.asp
Tông quan thì cách làm là tạo 2 classes (Code cho Class đã có trên link trên) sau đó trong mỗi sub thì sẽ theo cấu trúc như sau:
Mã:
[COLOR=#000000][FONT=&quot]Option Explicit[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]Dim mUndoClass As clsExecAndUndo

[/FONT][/COLOR]sub abc()
    [COLOR=#000000][FONT=&quot]    If mUndoClass Is Nothing Then[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]        Set mUndoClass = New clsExecAndUndo[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]    Else[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]        'Previous undoset, must be removed[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]        Set mUndoClass = Nothing[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]        Set mUndoClass = New clsExecAndUndo[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]    End If
[/FONT][/COLOR]
      ' lam cong viec gi do

[COLOR=#000000][FONT=&quot]    Application.OnUndo "Restore colours A1:A10", "UndoChange"[/FONT][/COLOR]
end sub

[COLOR=#000000][FONT=&quot]Sub UndoChange()[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]    If mUndoClass Is Nothing Then Exit Sub[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]    mUndoClass.UndoAll[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]    Set mUndoClass = Nothing[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]End Sub[/FONT][/COLOR]

Phương pháp này chạy ổn trong khuôn khổ 1 file Excel nhưng khi em viết AddIn thì ko hiểu sao phương pháp này ko hoạt động (sau khi chạy 1 sub trong file xlam thì ko Undo được nữa)

Mong các tiền bối giải thích cho em tại sao lại bị hiện tượng như vậy ;;;;;;;;;;;;;;;;;;;;;;
 
Web KT
Back
Top Bottom