Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3817|回复: 4

如何把TXT文档内容按行写入xls

[复制链接]
发表于 2011-7-3 13:45 | 显示全部楼层 |阅读模式
如题,请大家帮忙,谢谢!
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2011-7-3 13:49 | 显示全部楼层
SFile = fdf
Workbooks.Add
    Columns("A:F").Select
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Columns("A:A").Select
    Selection.NumberFormatLocal = "@"----设置了文本模式,filecopy之后,xls里还是常规
    Selection.ColumnWidth = 11.5
    Columns("B:B").Select
    Selection.ColumnWidth = 17.7
    Columns("D:D").Select
    Selection.ColumnWidth = 16
    Columns("E:E").Select
    Selection.ColumnWidth = 8.71
    Columns("F:F").Select
    Selection.ColumnWidth = 9.29
    Range("A1").Select
    'ActiveWorkbook.RemovePersonalInformation = True
    ActiveWorkbook.RemovePersonalInformation = False
    ActiveWorkbook.SaveAs Filename:=Splitpath & fn & ".xls", FileFormat:= _
        xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
        , CreateBackup:=False
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    DFile = Splitpath & fn & ".xls"   
    FileCopy SFile, DFile
回复

使用道具 举报

发表于 2011-7-3 14:21 | 显示全部楼层
回复

使用道具 举报

发表于 2011-7-3 14:26 | 显示全部楼层
看不懂二楼的,能不能简单些……
回复

使用道具 举报

发表于 2011-7-4 14:34 | 显示全部楼层
这个你可以试试

Sub test()
Dim i As Integer
Dim nPerCol As Integer
Dim textline As String
Dim strTemp As String
Open "E:\VBA\gds.txt" For Input As #1
i = 1
While Not EOF(1)
Line Input #1, textline
strTemp = textline
nPerCol = 1 '默认从第一列开始写
Do Until InStr(strTemp, vbTab) = 0 '判断是否含有Tab键
If InStr(strTemp, vbTab) = 1 Then
If Len(strTemp) > 1 Then
strTemp = Right(strTemp, Len(strTemp) - 1) '去掉第一个Tab
Else: strTemp = ""
End If
Else: Cells(i, nPerCol) = Left(strTemp, InStr(strTemp, vbTab) - 1)
strTemp = Right(strTemp, Len(strTemp) - InStr(strTemp, vbTab) + 1)
End If
nPerCol = nPerCol + 1 '无论是去掉一个Tab 还是写入一个值,默认写入列+1
Loop
If Len(strTemp) > 0 Then
Cells(i, nPerCol).Value = strTemp
End If
i = i + 1
Wend
Close #1
End Sub
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-14 21:51 , Processed in 0.287459 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表