Excel VBA作为全局变量的问题

时间:2015-04-18 09:13:10

标签: excel vba excel-vba global-variables

我有UserForm,我用它来快速输入数据。当UserForm初始化时,为不同的宏分配了一些快捷方式。其中一个宏从我的数据表中删除整行。应该使用另一个宏来恢复这种删除。为此,我使用范围作为全局变量来备份删除。但是,范围不在范围内。

请参阅相关代码段和错误:

表单代码:

Public Sub UserForm_Initialize()

    'set shortcuts
    '...

    Application.OnKey "{BACKSPACE}", "delete_row"
    Application.OnKey "^z", "redo_row"
    '...

    '<curRow> is global variable (in Module1) which is needed for PicBro Userform..
    curRow = ActiveCell.Row

    '...       
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

    '...

    'release shortcuts
    Application.OnKey "{BACKSPACE}"
    Application.OnKey "^z"
    '...

End Sub

第1单元代码:

'global variables in module1
'****************************
Public curRow As Long
Public rngBU As Range
'**************************** 
Public Sub delete_row()

    '...
    Set rngBU = Data.Rows(curRow)
    MsgBox "Copied row " & rngBU.Row & " for backup.."

    Data.Rows(curRow).delete Shift:=xlUp
    Data.Cells(curRow, 1).Activate

    '...
End Sub

Public Sub redo_row()
    MsgBox "Restoring deleted row " & rngBU.Row & " from backup.." 'ERROR runtime 424
    rngBU.Copy
    Data.Rows(curRow).Insert Shift:=xlDown
End Sub

0 个答案:

没有答案