Blog

利用VBA实现EXCEL数据输出TXT等文本文件

VBA----Visual Basic For Application的简称,属于VB的一个子集,广泛应用于Word套件的自动化,其寄存于现有的EXCEL或word等的文件里面 日常生活中,需要做到有逻辑性质的筛选EXCEL数据再进行输出的话往往比较麻烦,由此利用到EXCEL里面的宏,就是VBA编程就可以大大方便处理速度。今天举的例子是读取excel各个表格信息再进行处理输出。 现有表格一个 A      B      C 1   名字   数学   语文 2  motta    85     90 3   CCJK    80     75 4  Transee   99     88 需求:现在需要输出成一个TXT文本文件,里面格式如下 motta,85,90 CCJK,80,75 Transee,99,88 在excel2003或excel2007中打开宏编辑器 VBA使用VB语法,高级语言语法大同小异,具体函数可以查询相应API Dim MyTXT As String, Path As String     ‘定义输出的TXT文件 Dim h As Long, Data1(100) As Single, Data2(100) As Single, Data3(100) As _ Single Sub CopyTXT() On Error GoTo 0 '出现错误就结束 Name = InputBox("输入要存储的文本文件名称(不需加.txt)。") '自定义存储名称 Path = Application.ThisWorkbook.Path & Application.PathSeparator '与表格路径相同 MyTXT = Path & Name & ".txt"        '要转存的TXT文件全称 '先定义1~3列1~3行中的单元格的数据为指定的变量Data1(1)至Data3(3) For h = 1 To 3 Data1(h) = Cells(h, 1) Data2(h) = Cells(h, 2) Data3(h) = Cells(h, 3) Next h '将预定的变量值逐个输入到TXT文件,仅针对你上传的样式7列11行,格式为数据之间逗号分隔,按行排列 Open MyTXT For Output As #1 For h = 2 To 4 Print #1, Data1(h) & "," & Data2(h) & "," & Data3(h) Next h Close #1 0 End Sub 解析:#1为其文件流,里面的相应处理可以按照你的需要进行编写相关的程序,之后关闭,理论上文本类文件都可以输出不单只是txt,xml或lrc等之类都是可以的。 编写完成后,在宏那里选择执行。

Words translated by CCJK

146,096,379

Request a call or contact us

  •  

    Call ccjk.support for professional assistance

  •  

    Click Here to leave us a message and be as detailed as possible.

We are Certified

Our Client Satisfaction

rating for previous quarte

4.00
  •  
    Inquiry Form

    Please take few minutes to fill
    inquiry form and get Free Quote

  •  
    Request a Call

    Click to receive Free Call, Our
    Representative will contact you

  •  
    Leave a Message

    Please leave us a message and
    be as detailed as possible

Over 95% of our clients recommend our language services to others


Copyright © CCJK Technologies Co., Ltd. 2000-2017. All rights reserved.
TOP