您好,我想请问如何使用vba将某文件夹下的所有excel文件的sheet2另存为csv格式的,谢谢 5
直接保存成csv的也可以,我需要用这种格式的数据,但是因为文件太多了,而且每一个文件都不只一个sheet表,需要的只是sheet2,想可不可以写个宏让它自己存,谢谢...
直接保存成csv的也可以,我需要用这种格式的数据,但是因为文件太多了,而且每一个文件都不只一个sheet表,需要的只是sheet2,想可不可以写个宏让它自己存,谢谢
展开
展开全部
'文件夹内新建一工作簿将下列代码拷贝到模块中,关闭工作簿,
'再打开工作簿在菜单栏上有“保存CSV文件”,点击后将sheet2表批量生成CSV文件。
Sub SaveText1()
Dim File As String
wj = Dir(ThisWorkbook.Path & "\*.xls")
For i = 1 To 500 '根据文件数量
Workbooks.Open ThisWorkbook.Path & "\" & wj
x = Len(wj)
x = x - 4
wj = Left(wj, x)
File = ThisWorkbook.Path & "\" & wj & ".csv"
On Error Resume Next
Kill File '将原同名文件删除
Worksheets("Sheet2").Select
ActiveWorkbook.SaveAs Filename:=File, FileFormat:=xlCSV
ActiveWorkbook.Close SaveChanges:=False
If wj = "" Then End
wj = Dir
Next
End Sub
Sub auto_open()
MenuBars(xlWorksheet).Reset
Set mycommandbar = CommandBars("standard")
Set mybutton = mycommandbar.Controls.Add(Type:=msoControlButton)
With mybutton
.Style = msoButtonCaption
.Caption = "保存CSV文件"
.Enabled = True
.OnAction = "SaveText1"
End With
End Sub
Sub auto_close()
Set mycommandbar = CommandBars("standard")
For Each mybutton In mycommandbar.Controls
If mybutton.Caption = "保存CSV文件" Then mybutton.Delete
Next
End Sub
'再打开工作簿在菜单栏上有“保存CSV文件”,点击后将sheet2表批量生成CSV文件。
Sub SaveText1()
Dim File As String
wj = Dir(ThisWorkbook.Path & "\*.xls")
For i = 1 To 500 '根据文件数量
Workbooks.Open ThisWorkbook.Path & "\" & wj
x = Len(wj)
x = x - 4
wj = Left(wj, x)
File = ThisWorkbook.Path & "\" & wj & ".csv"
On Error Resume Next
Kill File '将原同名文件删除
Worksheets("Sheet2").Select
ActiveWorkbook.SaveAs Filename:=File, FileFormat:=xlCSV
ActiveWorkbook.Close SaveChanges:=False
If wj = "" Then End
wj = Dir
Next
End Sub
Sub auto_open()
MenuBars(xlWorksheet).Reset
Set mycommandbar = CommandBars("standard")
Set mybutton = mycommandbar.Controls.Add(Type:=msoControlButton)
With mybutton
.Style = msoButtonCaption
.Caption = "保存CSV文件"
.Enabled = True
.OnAction = "SaveText1"
End With
End Sub
Sub auto_close()
Set mycommandbar = CommandBars("standard")
For Each mybutton In mycommandbar.Controls
If mybutton.Caption = "保存CSV文件" Then mybutton.Delete
Next
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询