VBA Copy & Paste 3000 rows

江枫思渺然 提交于 2019-12-14 02:41:10

问题


my code below, whoch I've copied from a Yahoo Developers articles and changed accordingly, for Query, I want to copy and paste 2999 rows of insert statements from excel to Teradata. My current way doesn't copy and paste the entire range. If I swap this for: Cells(1, 1) & " " & Cells(2, 1) & " " & Cells(3, 1)....etc. until Cells(2999), it would work. A clever, simpler way of doing this please?

As an aside, would you recommend an alternative method of inserting 2999 rows. The tables are already populated, so FLOAD won't work. MLOAD or BTEQ? I'm using normal insert statements because 2999 is small enough to get away with. But, I'd always be very grateful for a q quicker solution! Thank you all!

Sub Insert_to_TD()



Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim cmdsqldata As ADODB.Command
Set cmdsqldata = New ADODB.Command

cn.Open "DSN=NNNNNN; Username=XXXXX; Password=YYYYYYY;"


Set cmdsqldata.ActiveConnection = cn 'This line says to which database it has to send the query
Query = Range(Cells(1, 1), Cells(2999, 1))
cmdsqldata.CommandText = Query 'We asign the query as command text
cmdsqldata.CommandType = adCmdText 'We just say what kind of command VBA has to execute
cmdsqldata.CommandTimeout = 0 'With this instruction we don't set any timeout, so the     query can take all the necessary time to be executed
Set rs = cmdsqldata.Execute() 'VBA just run the query and send back the result


End Sub

回答1:


This will cause no error, using VBA Join():

Function GetColumn1(varArray)
  Dim i, i0, i1, varArrayRet

  i0 = LBound(varArray, 1)
  i1 = UBound(varArray, 1)

  ReDim varArrayRet(i0 To i1)

  For i = i0 To i1
    varArrayRet(i) = varArray(i, 1)
  Next
  GetColumn1 = varArrayRet
End Function

Sub Insert_to_TD()

  Dim cn As ADODB.Connection
  Set cn = New ADODB.Connection
  Dim rs As ADODB.Recordset
  Set rs = New ADODB.Recordset
  Dim cmdsqldata As ADODB.Command
  Set cmdsqldata = New ADODB.Command

  Dim varArray, Query

  cn.Open "DSN=NNNNNN; Username=XXXXX; Password=YYYYYYY;"


  Set cmdsqldata.ActiveConnection = cn 'This line says to which database it has to send the query

  '
  'Query = Range(Cells(1, 1), Cells(2999, 1))
  '
  varArray = Range("A1:A2999").Value
  varArray = GetColumn1(varArray)
  Query = Join(varArray, " ")

  cmdsqldata.CommandText = Query 'We asign the query as command text
  cmdsqldata.CommandType = adCmdText 'We just say what kind of command VBA has to execute
  cmdsqldata.CommandTimeout = 0 'With this instruction we don't set any timeout, so the     query can take all the necessary time to be executed
  Set rs = cmdsqldata.Execute() 'VBA just run the query and send back the result


End Sub

Reserve:

Although you'd better use a for loop, insert your data line/line, it will be faster and better done than a bundled insert's.

Now we try run SQL line by line?

Sub Insert_to_TD()

  Dim cn As ADODB.Connection
  Set cn = New ADODB.Connection
  Dim rs As ADODB.Recordset
  Set rs = New ADODB.Recordset
  Dim cmdsqldata As ADODB.Command
  Set cmdsqldata = New ADODB.Command

  Dim i, strSQL

  cn.Open "DSN=NNNNNN; Username=XXXXX; Password=YYYYYYY;"


  Set cmdsqldata.ActiveConnection = cn

  cmdsqldata.CommandType = adCmdText
  cmdsqldata.CommandTimeout = 0

  '
  For i = 1 To 2999
    strSQL = ActiveSheet.Cells(i, 1).Value
    cmdsqldata.CommandText = strSQL
    Set rs = cmdsqldata.Execute()
  Next

End Sub


来源:https://stackoverflow.com/questions/19919586/vba-copy-paste-3000-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!