Excel精英培训网

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

[已解决]请帮忙看下这个代码如何修改下让它只读取前9行数据呢?

[复制链接]
发表于 2017-6-28 17:18 | 显示全部楼层 |阅读模式
Sub 读取数据()
    fileToOpen = Application.GetOpenFilename("文本(*.txt), *.txt", , "读取数据")
    If fileToOpen = False Then
        Exit Sub
    End If
    Range("A1").Select
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fileToOpen, Destination:=Range("A1"))
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 936
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub


最佳答案
2017-6-29 17:58
Sub test()
    Dim A, x, y

    '1)获取路径
    x = Application.GetOpenFilename("文本(*.txt), *.txt", , "读取数据")
    If x = False Then Exit Sub

    '2)读文本
    Open x For Input As #1
    A = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
    Close #1

    '3)导入工作簿
    A = Application.Transpose(A)
    x = 10: y = 19
    For i = x To y
        A(i - x + 1, 1) = A(i, 1)
    Next i
    Cells.Clear
    [a1].Resize(y - x + 1) = A

End Sub

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2017-6-29 10:08 | 显示全部楼层
Sub test()
    Dim x, A
   
    '1)获取路径
    x = Application.GetOpenFilename("文本(*.txt), *.txt", , "读取数据")
    If x = False Then Exit Sub
   
    '2)读文本
    Open x For Input As #1
    A = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
    Close #1
   
    '3)导入工作簿
    [a1].Resize(9) = Application.Transpose(A)
   
End Sub
回复

使用道具 举报

 楼主| 发表于 2017-6-29 17:32 | 显示全部楼层
本帖最后由 qinshuai8507 于 2017-6-29 17:38 编辑
爱疯 发表于 2017-6-29 10:08
Sub test()
    Dim x, A
   

非常好完美实现,谢谢大侠,请问如何再修改一下可以读取10-19行呢?
回复

使用道具 举报

发表于 2017-6-29 17:58 | 显示全部楼层    本楼为最佳答案   
Sub test()
    Dim A, x, y

    '1)获取路径
    x = Application.GetOpenFilename("文本(*.txt), *.txt", , "读取数据")
    If x = False Then Exit Sub

    '2)读文本
    Open x For Input As #1
    A = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
    Close #1

    '3)导入工作簿
    A = Application.Transpose(A)
    x = 10: y = 19
    For i = x To y
        A(i - x + 1, 1) = A(i, 1)
    Next i
    Cells.Clear
    [a1].Resize(y - x + 1) = A

End Sub

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-2 06:04 , Processed in 0.655612 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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