|
本帖最后由 好奇心 于 2014-12-27 21:42 编辑
附件的“目标.xls”的代码,已检查出“myFile.csv“文件的总行数,希望读入k=总行数/3(取整)行的“myFile.csv“文件的数据,放入“目标.xls”的”Sheet2.Rows(2)“中。
附件的“目标.xls”的代码:
Private Sub CommandButton1_Click()
Dim myCon, myRst, myCnc As String
Dim myCmd As String, myFileName As String
Dim i As Long, j As Integer, k As Integer
Set myCon = CreateObject("adodb.connection")
Set myRst = CreateObject("adodb.Recordset")
myFileName = "myFile.csv"
myCnc = "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"DBQ=" & ThisWorkbook.Path & "\;"
myCmd = "SELECT * FROM " & myFileName
myCon.Open "Provider=MSDASQL;" & myCnc
myRst.CursorLocation = 3
myRst.Open Source:=myCmd, ActiveConnection:=myCon
With myRst
i = .RecordCount
j = .Fields.Count
' MsgBox "行数=" & i & Chr(10) & "列数=" & j
k = i \ 3
'如何写代码,读入k行的“myFile.csv“文件的数据,放入“目标.xls”的”Sheet2“的第二行中。
End With
myCon.Close
Set myRst = Nothing
Set myCon = Nothing
End Sub
ADO.rar
(736.97 KB, 下载次数: 20)
|
|