2016-12-05 143 views
0

我想從我的電腦中選擇一個excel文件並將其插入到數據庫中。我有一些代碼可以正常工作,但是當我點擊取消按鈕時,它不會取消它,並且保持與打開按鈕一樣的工作。VBA按鈕不起作用

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

哪一行的錯誤? – user1

+0

當提示打開不顯示錯誤它工作,但並點擊取消按鈕然後將其插入數據庫 –

+0

你能告訴我請你所看到的 – user1

回答

0

promt發生在哪裏? 通常情況下,你必須告訴機器在「取消」上做什麼。像:

If [PROMPT] = vbNullString Then 
    Exit Sub 
End If 
+0

它顯示類型不匹配錯誤 –

+0

線8沒有提示發生 –

+0

「.Formula = MYDATA」分配路徑變量?在VBA – snibbo