【问题标题】:VBS script for opening text file and taking select parts and putting them into excel用于打开文本文件并选取部分并将它们放入 excel 的 VBS 脚本
【发布时间】:2015-10-01 13:38:13
【问题描述】:

例如,我将在文本文件中有这一行: RTL N23 U 194.0 |系统 N24 U 978.9 744.3 |等等

我想复制该值直到“|”然后插入到excel文件中。

任何帮助或想法将不胜感激。

【问题讨论】:

    标签: excel text vbscript


    【解决方案1】:

    我建议打开 Excel,启用开发人员工具并使用宏录制来记录您想要的那些步骤。

    之后,停止录制,您将获得 vbscript 的基础知识。

    以后您需要对代码进行一些调整,我一定可以帮助您。

    【讨论】:

    • 这是一个文本文件,因此很难在 excel 中对其进行操作。我只需要将某些值从文本文件插入到 Excel 单元格中。
    • 贴一段这个文本文件,我可以给你一个开始。
    • RTL N23 U 194.0 |系统 N24 U 978.9 744.3 | ADA N24 U 109.0 108.9 |亚行 N24 U 107.4 107.5
    【解决方案2】:

    好的,这应该让你开始。

    请注意,我将数据所在的文件命名为 data.txt。

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    
    '
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\Temp\data.txt", _
            Destination:=Range("$A$1"))
            .CommandType = 0
            .Name = "data"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileOtherDelimiter = "|"
            .TextFileColumnDataTypes = Array(1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    
        Rows("1:1").Select
        Selection.Copy
        Range("A2").Select
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
        Rows("1:1").Select
        Application.CutCopyMode = False
        Selection.Delete Shift:=xlUp
        Range("A1").Select
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2020-04-23
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      相关资源
      最近更新 更多