Ich möchte eine Excel-Datei von meinem Computer auswählen und in eine Datenbank einfügen. Ich habe etwas Code, der richtig funktioniert, aber wenn ich auf den Abbrechen-Knopf klicke, annulliert er es nicht und macht die gleiche Arbeit wie geöffneter Knopf.VBA-Taste funktioniert nicht
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
Welche Zeilenfehler? – user1
nicht zeigen Fehler es funktioniert, aber wenn prompt öffnen und klicken Sie auf Abbrechen, dann in die Datenbank einfügen –
können Sie mir einen Screenshot zeigen, was Sie sehen – user1