使用macrosVBA将Excel中的200,000行导入SQL

当通过macros将信息从Excel传递到SQL时,我遇到问题。

当您运行macros时,文件开始上传行,但突然停止,出现以下错误“溢出(6)”,这使得每32676行处理。

起初我以为是logging中的东西,但是当我回到执行句子时,又重新开始logging32676,

我试图上传一个超过20万行的Excel文件,每个logging由25列组成。

我可能会做这个事情发生错误。

我分享交易代码。

Private Sub CommandButton1_Click() Dim conn As New ADODB.Connection conn.CommandTimeout = 0 Dim iRowNo As Integer Dim PK_ID As String, CUST_ID As Long, DOC_TYPE As String, BILL_DOC As Long, REFERENCE As String, INV_CON As String, ORDER_SALER As String, INV_REF As String, DOC_NUM As Long, GL As Long, DOC_DATE As Double, DUE_DATE As Double, ECURRENCY As String, DOC_AMNT As Currency, USD_AMNT As Currency, PAY_TERM As String With Sheets("ONREPSAP") 'Abrimos conexion con el SQL server conn.Open "Provider=SQLOLEDB;Data Source=,1433;Initial Catalog=AmericanMovil;User ID=;Password=;Encrypt=True;" 'Omitimos la linea de encabezados iRowNo = 2 'Loop until empty cell in CustomerId Do Until .Cells(iRowNo, 1) = "" PK_ID = .Cells(iRowNo, 1) CUST_ID = .Cells(iRowNo, 2) DOC_TYPE = .Cells(iRowNo, 3) BILL_DOC = .Cells(iRowNo, 4) REFERENCE = .Cells(iRowNo, 5) INV_CON = .Cells(iRowNo, 6) ORDER_SALES = .Cells(iRowNo, 7) INV_REF = .Cells(iRowNo, 8) DOC_NUM = .Cells(iRowNo, 9) GL = .Cells(iRowNo, 10) DOC_DATE = .Cells(iRowNo, 11) DUE_DATE = .Cells(iRowNo, 12) ECURRENCY = .Cells(iRowNo, 13) DOC_AMNT = .Cells(iRowNo, 14) USD_AMNT = .Cells(iRowNo, 15) PAY_TERM = .Cells(iRowNo, 16) 'Generamos y ejecutamos la QUERY SQL para importar las lineas de excel a SQL conn.Execute "insert into dbo.ONREPSAP (PK_ID, CUST_ID, DOC_TYPE, BILL_DOC, REFERENCE, INV_CON, ORDER_SALES, INV_REF, DOC_NUM, GL, DOC_DATE, DUE_DATE, ECURRENCY, DOC_AMNT, USD_AMNT, PAY_TERM) values ('" & PK_ID & "', '" & CUST_ID & "', '" & DOC_TYPE & "', '" & BILL_DOC & "', '" & REFERENCE & "', '" & INV_CON & "', '" & ORDER_SALES & "', '" & INV_REF & "', '" & DOC_NUM & "', '" & GL & "', '" & DOC_DATE & "', '" & DUE_DATE & "', '" & ECURRENCY & "', '" & DOC_AMNT & "', '" & USD_AMNT & "', '" & PAY_TERM & "')" iRowNo = iRowNo + 1 Loop MsgBox "Reporte Cargado Correctamente | The report has finish to Uploading" conn.Close Set conn = Nothing End With End Sub 

在Debug中,VBA在“Loop”之前selectiRowNo + 1,

正如@Comintem说,你需要改变types,以保持更大的价值。

你应该声明Dim iRowNo As Long而不是Integer

这里数据types总结

 Integer uses 2 bytes and Range from -32,768 to 32,767 Long uses 4 bytes and Range from -2,147,483,648 to 2,147,483,647