在vb中将TXT档导入到EXCEL的问题,求代码

把D:\11.txt中的数据导入到D:\121.xlsx中的A列中,把D:\12.txt中的数据导入到D:\121.xlsx中的B列中。txt中都有一列数据。求详细代码,之前自己在网上找了,自己试一下有很多问题出现

没有分析你的txt文件,现在假定你的11.txt与12.txt文件的行数相同时才不会出错。
Private Sub Command1_Click()
Dim L1, L2 As String
Dim I As Integer
Open "d:\11.txt" For Input As #1
Open "d:\12.txt" For Input As #2
'从"工程"菜单中选择"引用"栏;选择Microsoft Excel 9.0 Object Library(EXCEL2000),然后选择"确定"。
Dim xlApp As Excel.Application
Dim xlBook As Excel.WorkBook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Open("D:\121.xls") '打开已经存在的EXCEL工件簿文件
xlApp.Visible = True '设置EXCEL对象可见(或不可见)
Set xlSheet = xlBook.Worksheets("sheet1") '设置活动工作表
Do While Not EOF(1)
Line Input #1, L1
Line Input #2, L2
I = I + 1
xlSheet.Cells(I, 1) = L1 '给单元格(row,col)赋值
xlSheet.Cells(I, 2) = L2 '给单元格(row,col)赋值
Loop
xlBook.Close (True) '关闭工作簿
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
End Sub追问

非常感谢您!!
行数是一样的……但是它会执行两遍xlSheet.Cells(I, 1) = L1 '给单元格(row,col)赋值
。我都有300行,最后执行的结果是A1有600行,B300行……相当于A1导入了两遍……

追答

怪了,我测试了三行没有问题呀,你是不是没有复制全,现在没空,晚上再和你聊。

追问

有点怪,我测试720行没问题,360行有问题……好的,有空您在找我

温馨提示:内容为网友见解,仅供参考
无其他回答

在vb中将TXT档导入到EXCEL的问题,求代码
Private Sub Command1_Click()Dim L1, L2 As String Dim I As Integer Open "d:\\11.txt" For Input As #1 Open "d:\\12.txt" For Input As #2 '从"工程"菜单中选择"引用"栏;选择Microsoft Excel 9.0 Object Library(EXCEL2000),然后选择"确定"。Dim xlApp As Excel.Application Dim...

如何用VB代码实现一次导入多个TXT文件到EXCEL中?求代码...
请将VB生成的EXE文件和txt文件放在同一个文件夹内,然后运行代码,最终所有的数据将汇总到同样目录下的“汇总工作簿”中。代码如下:Private Sub Command1_Click() Dim xlapp As Object Dim aimwb As Object, wb As Object Set xlapp = CreateObject("Excel.Application") Set aimwb =...

如何将VB中TEXT里的文本导入EXCEL里面啊
打开文本数据。新建Excel文档,并打开Excel文档,点击左上角开始处,点击“打开”。打开数据导入界面,选择文本数据点击“确定”,进入下一界面。在下一界面,勾选“逗号”,点击“下一步”,下一页面,继续点击“下一步”。操作完成后,数据导入完成。

如何用VBA导入txt格式文本到Excel
Application.ScreenUpdating = False Worksheets("Sheet1").Range("A1:Z65536").ClearContents my_path = ThisWorkbook.Path file_name = "test.txt"'读取文件 Open my_path & "\\" & file_name For Input As #1 lines = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)Close #1 k ...

请帮忙看看用VB把TXT数据导入EXCEL的代码哪里需要修改..谢谢_百度知...
MyName = Dir$(MyPath & "*.txt")Do While MyName <> ""'Workbooks.OpenText (MyPath & MyName)open MyPath & MyName for input as #1 'With ActiveWorkbook '.ActiveSheet.Range("A1").CurrentRegion.Copy sh.Range("a65000").End(xlUp).Offset(1)'.Close False 'End With i=1 do ...

求vb代码:批量txt导入,一个文本占一个单元格(某个文件夹里有上万个...
Private Sub Command1_Click()Dim oxl As Object, owb As Object, ost As ObjectDim d As String, i As LongSet oxl = CreateObject("Excel.Application")Set owb = oxl.Workbooks.AddSet ost = owb.sheets(1)d = Dir("f:\\test\\*.txt")Do Until d = "" i = i + 1 ost.ce...

vb.net把txt导入到excel
直接用Open语句即可!app.Workbooks.Open(filename:="D:\\xx.txt")如果要指定分隔符,比如“a-b-c”,将a,b,c三个字符分别存储到三个不同单元格,那么就要设置分隔符:app.Workbooks.Open(filename:="D:\\xx.txt",Format:=6,Delimiter:="-")Format:=6表示允许自定义分隔符 Delimiter:="-",...

如何用VB把txt文件指定内容写到EXCEL中
"\\test.txt"For Input As 1 Set exlApp = New Excel.Application exlApp.Visible = True Set exlBook = exlApp.Workbooks.Open(App.Path + "\\test.xls")'打开已有Excel文件 '向Excel表中写入数据,Sheets(1)为第1个工作表,Cells(行号,列号)是单元格 I = 0 While Not EOF(1)Line Input...

excel vb 扫描TEXT文件条件录入Excel
String, strfiletext As StringDim xrow As IntegerWith Application.FileDialog(msoFileDialogFolderPicker) If .Show Then strpath = .SelectedItems(1) Else Exit Sub End IfEnd Withstrfilename = Dir(strpath & "\\*.txt")xrow = 1 '设置一个在Excel中填入含关键字的内容的...

VBA多个txt批量导入到excel
试试下面的代码:Sub 导入指定行() Dim myfile Dim f As String, mypath As String f = ThisWorkbook.Path & "" & "*.txt" myfile = Dir(f) s2 = 1 Do While myfile <> "" mypath = ThisWorkbook.Path & "" & myfile ' On Error Resume Next Open...

相似回答
大家正在搜