VBA按钮不起作用

时间:2016-12-05 10:27:28

标签: sql excel vba

我想从我的计算机中选择一个excel文件并将其插入数据库。我有一些代码正常工作,但是当我点击取消按钮时,它不会取消它并继续执行与打开按钮相同的工作。

Sub GetDataFromClosedBook()
'On Error GoTo myError
Dim mydata As String
'data location & range to copy
mydata = "='C:\Users\IT.int08\Downloads\[close.xls]Sheet1'!$A1:$C1000" '<< change as required
'link to worksheet
With ThisWorkbook.Worksheets(1).Range("A1:C1000") '<< change as required

.Formula = mydata
'convert formula to text
.Value = .Value
Cells.Replace What:="'", Replacement:="", LookAt:=xlPart, SearchOrder:= _
        xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Dim conn As New ADODB.Connection
Dim Counter As String
Counter = Cells.Item(2, 4)

Dim IRow As Integer
IRow = Cells.Item(2, 4)
IRow = IRow + 1
Dim rgno, taxpname As String

        conn.Provider = "sqloledb"
        'conn.Properties("Prompt") = adPromptAlways
        conn.Open "Data Source=10.10.1.139;Initial Catalog=time;user id=sa;password=11"

        Do Until .Cells(IRow, 1) = 0 Or Null
            rgno = .Cells(IRow, 2)
            taxpname = .Cells(IRow, 3)
            Cells.Item(2, 4) = IRow
         Dim Qu As String
         If Len(rgno) >= 13 Then

            Qu = "insert into dbo.rough (NTN_no,CNIC,TAXPAYERNAME) values ('" & "" & "', '" & rgno & "', '" & taxpname & "')"
            conn.Execute (Qu)
            ElseIf Len(rgno) < 13 Then
            Dim asdf As String
            asdf = "insert into dbo.rough (NTN_no,CNIC,TAXPAYERNAME) values ('" & rgno & "', '" & "" & "', '" & taxpname & "')"
 conn.Execute (asdf)
 End If
 IRow = IRow + 1

 DoEvents      
 Loop       
'myError:
'MsgBox ("record is missing")
 Sheet1.Cells.Clear

End With
End Sub

1 个答案:

答案 0 :(得分:0)

promt在哪里发生? 通常,你必须告诉机器如何处理“取消”,smth。喜欢:

If [PROMPT] = vbNullString Then
    Exit Sub
End If