高分求用VBA将excel表格数据 导出固定格式的txt文件。
求VBA代码,将例如北京号码启用-49神州行13410001341000北京号码启用-49神州行13410001341001北京号码启用-49神州行13410001341...
求VBA代码,将例如
北京 号码启用-49 神州行 1341000 1341000
北京 号码启用-49 神州行 1341000 1341001
北京 号码启用-49 神州行 1341000 1341002
深圳 号码启用-49 神州行 1341000 1341003
用如下格式的TXT文件输出:每行像下面的格式,北京改为BJ,第二列不提取,神州行改为shengzhouxing,用|分开
BJ|shengzhouxing|1341000|1341003
我要处理的东西是一大排一大排的,如果要我复制,粘贴那我就弄到过年都没弄完。谢谢 展开
北京 号码启用-49 神州行 1341000 1341000
北京 号码启用-49 神州行 1341000 1341001
北京 号码启用-49 神州行 1341000 1341002
深圳 号码启用-49 神州行 1341000 1341003
用如下格式的TXT文件输出:每行像下面的格式,北京改为BJ,第二列不提取,神州行改为shengzhouxing,用|分开
BJ|shengzhouxing|1341000|1341003
我要处理的东西是一大排一大排的,如果要我复制,粘贴那我就弄到过年都没弄完。谢谢 展开
展开全部
给你大致写个思路吧
定义你要导出的文件名为result.txt,路径为D:\
源文件为d:\file.csv
Public Function OpenWorkBook(path As String) As Excel.Workbook
Dim xls As Excel.Workbook
For Each xls In Excel.Application.Workbooks
If InStr(path, xls.Name) Then
Excel.Application.DisplayAlerts = False
xls.Close
Excel.Application.DisplayAlerts = True
Exit For
End If
Next
Excel.Application.DisplayAlerts = False
Set OpenWorkBook = Excel.Workbooks.Open(path)
Excel.Application.DisplayAlerts = True
End Function
Public Function change()
Dim templateBook As Excel.Workbook
Dim templateFilePath As String
Dim SavePath as String
Dim fileName as String
Dim row AS Long
dim R1 AS Long
dim tempStr as String
templateFilePath = "d:\file.csv"
Set templateBook = OpenWorkBook(templateFilePath)
R1 = templateBook Sheets(1).UsedRange.Rows.Count
SavePath = "D:\"
fileName = "result.txt"
Open SavePath & fileName For Output As #1
For row = 1 To R1
if templateBook Sheets(1).cells(row,1).value = "北京" then
tempStr = "bj"
endif
if templateBook Sheets(1).cells(row,3).value = "神州行" then
tempStr = tempStr + "|shenzhouxing"
endif
.....
Print #1,tempStr
Next
Close #1
定义你要导出的文件名为result.txt,路径为D:\
源文件为d:\file.csv
Public Function OpenWorkBook(path As String) As Excel.Workbook
Dim xls As Excel.Workbook
For Each xls In Excel.Application.Workbooks
If InStr(path, xls.Name) Then
Excel.Application.DisplayAlerts = False
xls.Close
Excel.Application.DisplayAlerts = True
Exit For
End If
Next
Excel.Application.DisplayAlerts = False
Set OpenWorkBook = Excel.Workbooks.Open(path)
Excel.Application.DisplayAlerts = True
End Function
Public Function change()
Dim templateBook As Excel.Workbook
Dim templateFilePath As String
Dim SavePath as String
Dim fileName as String
Dim row AS Long
dim R1 AS Long
dim tempStr as String
templateFilePath = "d:\file.csv"
Set templateBook = OpenWorkBook(templateFilePath)
R1 = templateBook Sheets(1).UsedRange.Rows.Count
SavePath = "D:\"
fileName = "result.txt"
Open SavePath & fileName For Output As #1
For row = 1 To R1
if templateBook Sheets(1).cells(row,1).value = "北京" then
tempStr = "bj"
endif
if templateBook Sheets(1).cells(row,3).value = "神州行" then
tempStr = tempStr + "|shenzhouxing"
endif
.....
Print #1,tempStr
Next
Close #1
展开全部
这个用不着动用VBA,
直接在excel里面处理后复制粘贴在txt里面就可以了。
不用一个一个的复制,可以用公式的。
直接在excel里面处理后复制粘贴在txt里面就可以了。
不用一个一个的复制,可以用公式的。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可能没明白楼上的意思,你建一列==IF(Book2!A1="北京","BJ|")&IF(Book2!C1="神州行","shengzhouxing|")&Book2!D1&Book2!E1,让后一拖,拷贝一行,粘贴就可以了。
另外是不是深圳需要改为sz那?家园卡需要改为jiayuanka?
另外是不是深圳需要改为sz那?家园卡需要改为jiayuanka?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询