VBA宏Excel导出列,第一行标题显示数据
如何导出第一行中存在数据(标题)的所有列?
在此示例图像中,我只想导出存在 "FOO" 的列:
这是我的代码;
Sub Worksheets_to_txt()
Dim ws As Worksheet
Dim relativePath As String
Dim answer As VbMsgBoxResult
relativePath = ActiveWorkbook.Path
answer = MsgBox("Export in TXT?", vbYesNo, "Run Macro")
If answer = vbYes Then
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each ws In ActiveWorkbook.Worksheets
ws.Select
ws.Copy
ActiveWorkbook.SaveAs Filename:= _
relativePath & "\" & ws.Name & ".txt", _
FileFormat:=xlText, CreateBackup:=False
ActiveWorkbook.Close
ActiveWorkbook.Activate
Next
End If
End Sub
谢谢
How can I export all the columns with data present (header) in the first row?
In this example image, I would like to export only the column with "FOO" present:
this is my code;
Sub Worksheets_to_txt()
Dim ws As Worksheet
Dim relativePath As String
Dim answer As VbMsgBoxResult
relativePath = ActiveWorkbook.Path
answer = MsgBox("Export in TXT?", vbYesNo, "Run Macro")
If answer = vbYes Then
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each ws In ActiveWorkbook.Worksheets
ws.Select
ws.Copy
ActiveWorkbook.SaveAs Filename:= _
relativePath & "\" & ws.Name & ".txt", _
FileFormat:=xlText, CreateBackup:=False
ActiveWorkbook.Close
ActiveWorkbook.Activate
Next
End If
End Sub
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请将代码的这一部分替换
为:
它将没有标题的第一行单元格放置在一个范围中,并在保存工作簿之前立即删除该范围的整个列。
Please, replace this part of your code:
with this one:
It places the first row cells without a header in a range and delete this range entire columns at once, before saving the workbook.
这是我对这个问题的看法。我没有删除列,而是将它们收集在
ExportRange
中并将其复制到新文件中。Here is my take on this problem. Instead of removing columns, I'm collecting them in
ExportRange
and copy it to new file.