要从Excel 多个sheet内导出指定行为txt文件,懒得用C#了,写个VBA宏

 Sub Export()
Dim FileName As Variant
Dim Sep As String
Dim StartSheet As Integer
Dim EndSheet As Integer Dim ExportIndex As Integer '文件名
FileName = Application.GetSaveAsFilename(InitialFileName:=vbNullString, FileFilter:="Text Files (*.txt),*.txt")
If FileName = False Then
''''''''''''''''''''''''''
' user cancelled, get out
''''''''''''''''''''''''''
Exit Sub
End If
'分隔符
' Sep = Application.InputBox("Enter a separator character.", Type:=2) '开始Sheet
'StartSheet = Application.InputBox("开始Sheet.", Type:=2)
'结束Sheet
EndSheet = Application.InputBox("结束Sheet.", Type:=) '导出行
ExportIndex = Application.InputBox("导出行号.", Type:=) ShartSheet:=StartSheet, EndSheet:=EndSheet, ExportRow:=ExportIndex
ExportRangeToTextFile FName:=CStr(FileName), SelectionOnly:=False, AppendData:=False, _
ShartSheet:=, EndSheet:=EndSheet, ExportRow:=ExportIndex
End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 将Excel内多个Sheet中的某一行导出Text
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ExportRangeToTextFile(FName As String, _
SelectionOnly As Boolean, _
AppendData As Boolean, ShartSheet As Integer, _
EndSheet As Integer, ExportRow As Integer) Dim WholeLine As String
Dim FNum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer
Dim CellValue As String
Dim X As Variant Application.ScreenUpdating = False
On Error GoTo EndMacro:
FNum = FreeFile
Open FName For Output Access Write As #FNum For i = To Application.sheets.Count
X = Application.sheets(i).UsedRange.Value
WholeLine = ""
With Application.sheets(i).UsedRange
StartRow = .Cells().Row
StartCol = .Cells().Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With For j = To EndCol
WholeLine = WholeLine + X(ExportRow, j) + Chr("") '\t
Next
Print #FNum, WholeLine
Next
MsgBox "OK" '
EndMacro:
On Error GoTo
Application.ScreenUpdating = True
Close #FNum
'XT = Application.Transpose(X)转置 End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 导出单个sheet
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ExportSingleSheetToTextFile(FName As String, _
Sep As String, SelectionOnly As Boolean, _
AppendData As Boolean) Dim WholeLine As String
Dim FNum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer
Dim CellValue As String Application.ScreenUpdating = False
On Error GoTo EndMacro:
FNum = FreeFile If SelectionOnly = True Then
With Selection
StartRow = .Cells().Row
StartCol = .Cells().Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With
Else
With ActiveSheet.UsedRange
StartRow = .Cells().Row
StartCol = .Cells().Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With
End If If AppendData = True Then
Open FName For Append Access Write As #FNum
Else
Open FName For Output Access Write As #FNum
End If For RowNdx = StartRow To EndRow
WholeLine = ""
For ColNdx = StartCol To EndCol
If Cells(RowNdx, ColNdx).Value = "" Then
CellValue = Chr() & Chr()
Else
CellValue = Cells(RowNdx, ColNdx).Value
End If
WholeLine = WholeLine & CellValue & Sep
Next ColNdx
WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))
Print #FNum, WholeLine
Next RowNdx EndMacro:
On Error GoTo
Application.ScreenUpdating = True
Close #FNum End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 将Excel内多个Sheet中的某一行导出New Sheet
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ExportRangeToNewSheet(FName As String, _
SelectionOnly As Boolean, _
AppendData As Boolean, ShartSheet As Integer, _
EndSheet As Integer, ExportRow As Integer)
Dim FNum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer
Dim CellValue As String
Dim X As Variant
Dim Xsheet As Worksheet Set Xsheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
Xsheet.Name = FName 'Format(Now(), "HHmmss") Application.ScreenUpdating = False Dim index As Integer
index =
'For i = 1 To Application.Sheets.Count
For i = ShartSheet To EndSheet 'Application.Sheets.Count
With Application.Sheets(i).UsedRange
EndCol = .Cells(.Cells.Count).Column
For j = To EndCol
Xsheet.Cells(j, * index - ).Value = .Cells(, j).Text
Xsheet.Cells(j, * index).Value = .Cells(ExportRow, j).Text
Next
End With
index = index +
Next
MsgBox "导出OK,Sheet名" + FName '
'XT = Application.Transpose(X)转置 End Sub

//从text文件导入Excel sheet里面

Sub OpenFile()

 Dim filter As String
Dim fileToOpen filter = "All Files(*.*),*.*,Word Documents(*.do*),*.do*," & _
"Text Files(*.txt),*.txt"
fileToOpen = Application.GetOpenFilename(filter, 4, "请选择文件") If fileToOpen = False Then
MsgBox "你没有选择文件", vbOKOnly, "提示"
Else ' Workbooks.Open FileName:=fileToOpen
' MsgBox "你选择的文件是:" & fileToOpen, vbOKOnly, "提示"
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" + fileToOpen, Destination:=Range("$A$1") _
)
.Name = "Sample"
.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 = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If
End Sub

  

vba: Importing text file into excel sheet

http://blog.csdn.net/ldwtill/article/details/8571781

Using a QueryTable

Sub Sample()
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Sample.txt", Destination:=Range("$A$1") _
)
.Name = "Sample"
.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 = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Open the text file in memory Sub Sample()
Dim MyData As String, strData() As String Open "C:\Sample.txt" For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
strData() = Split(MyData, vbCrLf)
End Sub
Once you have the data in the array you can export it to the current sheet. Using the method that you are already using Sub Sample()
Dim wbI As Workbook, wbO As Workbook
Dim wsI As Worksheet Set wbI = ThisWorkbook
Set wsI = wbI.Sheets("Sheet1") '<~~ Sheet where you want to import Set wbO = Workbooks.Open("C:\Sample.txt") wbO.Sheets(1).Cells.Copy wsI.Cells wbO.Close SaveChanges:=False
End Sub
FOLLOWUP You can use the Application.GetOpenFilename to choose the relevant file. For example... Sub Sample()
Dim Ret Ret = Application.GetOpenFilename("Prn Files (*.prn), *.prn") If Ret <> False Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & Ret, Destination:=Range("$A$1")) '~~> Rest of the code End With
End If
End Sub

  

Excel 导出指定行为txt文件(VBA,宏)的更多相关文章

  1. 导出OpenID为txt文件的方法

    导出OpenID为txt文件的方法 public function export(){ $shop = M("Shop"); $arr = $shop->field('ope ...

  2. Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)

    功能说明 输入文件路径,在浏览器输出文件预览信息,经测试极速(Chrome).IE9.Firefox通过 分类文件及代码说明  DemoFiles 存放可测试文件 Default.aspx  启动页 ...

  3. excel、csv、txt文件数据读取

    /// <summary> /// 读取Excel表每一行第一列的字符串集合 /// </summary> /// <param name="filePath& ...

  4. winform NPOI excel 导出并选择保存文件路径

    public void ExcelOp(DataGridView gdv,ArrayList selHead) { if (selHead.Count==0) { MessageBox.Show(&q ...

  5. SVN导出指定版本差异文件 ***

    当一个项目进入运营维护阶段以后,不会再频繁地更新全部源文件到服务器,这个时间的修改大多是局部的,因此更新文件只需更新修改过的文件,其他 没有修改过的文件就没有必要上载到服务器.但一个稍微上规模的项目文 ...

  6. SAS 读取指定目录下文件列表宏

    OPTIONS PS=MAX LS=MAX NOCENTER SASMSTORE=SASUSER MSTORED MAUTOSOURCE;/*获取指定文件夹的指定类型的所有文件*/%MACRO GET ...

  7. mysql 导出行数据到txt文件,指定字符分割

    select id,name, concat('tel:',phone) from user order by time INTO outfile 'user.txt' FIELDS terminat ...

  8. dataview将excel表格的数据导出成txt文件

    有时候需要处理大量的数据,且这些数据又存在于excel表格内,在平时的时候,我是非常喜欢这样的数据的,因为只要是excel表格内的数据,处理起来的方法就很方便.也可能我平时遇见的数据总是以一种杂乱无章 ...

  9. 将指定目录中的txt文件转化成excel文件

    #!/usr/bin/env python#coding:utf-8import reimport osimport globimport xlwtimport sysdir=r"F:\te ...

随机推荐

  1. 设计模式--策略模式(strategy)

    1.策略模式(strategy ['strætədʒi]) 我的理解是:方案候选模式 (反正关键就是有很多的候选,哈哈) 看了很多例子,都是在说鸭子的,那个例子很好,在这里可以看 他们生产鸭子,我们就 ...

  2. You have not concluded your merge. (MERGE_HEAD exists)。(转)

    自己简直就是一个git小白,碰到问题,一点点的解决吧,可能不太系统,但也只能勤能补拙了 Git本地有修改如何强制更新 本地有修改和提交,如何强制用远程的库更新更新.我尝试过用git pull -f,总 ...

  3. 王亮:游戏AI探索之旅——从alphago到moba游戏

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由云加社区技术沙龙 发表于云+社区专栏 演讲嘉宾:王亮,腾讯AI高级研究员.2013年加入腾讯,从事大数据预测以及游戏AI研发工作.目前 ...

  4. 解决angular-deckgrid高度不均衡和重加载的问题

    在项目中使用angular-deckgrid+ng-infinite-scroll实现瀑布流的无限加载.但是实际测试中发现deckgrid有2个比较严重影响体验的BUG: 每次添加新的card,整个d ...

  5. python爬虫实战(九)--------拉勾网全站职位(CrawlSpider)

    相关代码已经修改调试成功----2017-4-24 详情代码请移步我的github:https://github.com/pujinxiao/Lagou_spider 一.说明 1.目标网址:拉勾网 ...

  6. ASP.Net之一般处理程序

    1.静态语言和动态语言 静态语言:在服务器端,不会被执行,直接作为 字符串 发回给浏览器,由浏览器运行的语言( HTML+CSS+JS).   动态语言:在服务端,会被服务器端的某种语言的虚拟机执行的 ...

  7. [linux] C语言Linux系统编程-做成守护进程

    守护进程: 必须是init进程的子进程,运行在后台,不与任何控制终端相关联. 通过以下步骤成为守护进程 1.调用fork()创建出来一个新的进程,这个新进程会是将来的守护进程 2.在新守护进程的父进程 ...

  8. BZOJ P1188 HNOI2007 分裂游戏——solution

    题目描述: (<--这个) 组合游戏,——把每个石头看做一个游戏, Multi_game——消去i上的石子后,,k上的游戏又多了一个: 于是就套用multi_game的模型即可 求解SG函数时, ...

  9. Hibernate详讲

    一 概述 1.JPA Java Persistence API,是Java EE为ORM框架定义的规范,任何使用java语言的ORM框架都必须实现该规范.Hibernate/Mybatis都是是JPA ...

  10. NGINX本地服务器解析域名

    1.找到hosts文件 ,添加需要解析的域名 2.在cmd命令窗口中检测解析是否生效 3 找到本地服务器的域名配置文件:添加绑定的域名,更改访问的目录 4.添加pathinfo.隐藏index.php ...