VBA 全局变量的问题
OnB 是 B 的回调函数,我是想定义一个全局变量rowIndex,然后在OnB被调用时用到这个值,
报错出在rowIndex=2这句话了,有人知道为什么吗,刚刚接触VBA
Option ExplicitDim WithEvents CSSS As BAPI;Dim tradeSheet As WorksheetDim tradeRow As LongPublic rowIndex As IntegerrowIndex = 2Private Sub CommandButton1_Click() Dim ErrorID As Long Set tradeSheet = ThisWorkbook.Sheets("trade_record") tradeRow = 4 If CSSS Is Nothing Then Set CSSS = New COMLIB.BAPI End If Dim frm As UserForm1 Set frm = New UserForm1 frm.Show If Len(frm.txtUserName) > 0 Then Call CSSS.Login(frm.txtUserName.Value, frm.txtPassword.Value, ErrorID) If ErrorID <> 0 Then MsgBox "Failed to Login, ErrorID:" & ErrorID Set ctp = Nothing End If End IfEnd SubPrivate Sub CommandButton2_Click() Call CSSS.CloseEnd Sub'when com finished initialized;Private Sub OnConnected() Dim str As String str = "xxxxx" Call B(str)End SubPrivate Sub OnB() Cells(rowIndex, 1).Value = "XX" 'rowIndex = rowIndex + 1End Sub
Private Sub Document_Open() rowIndex = 2End Sub