我有大约40个文件,可以使用Excel打开,它们是IQy文件,我正在尝试浏览所有文件并将其另存为xlsx文件。我到目前为止在VBA中拥有的是这个
Sub ConvertFiles()
Dim Filename, Pathname As String
Dim wb As Workbook
Pathname = "C:\Users\CHI\Downloads"
Filename = Dir(Pathname & ".iqy")
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename)
wb.SaveAs Pathname & Filename & ".xlsx"
wb.Close
Filename = Dir()
Loop
End Sub
据我了解,这会循环遍历我的下载文件,该文件存储了iqy文件,然后以xlsx格式另存为。当我运行它时,什么也没发生。
更新
Sub ConvertFiles()
Dim Filename, Pathname As String
Dim wb As Workbook
Pathname = "C:\Users\CHI\Downloads\"
Filename = Dir(Pathname & "*.iqy")
Application.DisplayAlerts = False
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename)
wb.SaveAs Pathname & Filename & ".xlsx", FileFormat:=51
wb.Close
Filename = Dir()
Loop
End Sub
这是对我有用的方法,我现在唯一的问题是在更改每个文件后,我会提示我导入数据,而我只需要按即可。有没有一种方法可以使这部分自动化,以便我可以使用table选项导入数据。
答案 0 :(得分:2)
您需要包括通配符才能找到iqy文件,并且您的路径名将需要附加的文件夹分隔符以使Open和SaveAs起作用:
Sub ConvertFiles()
Dim Filename, Pathname As String
Dim wb As Workbook
Pathname = "C:\Users\CHI86786\Downloads\"
Filename = Dir(Pathname & "*.iqy")
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename)
wb.SaveAs Pathname & Filename & ".xlsx", FileFormat:=xlOpenXMLWorkbook
wb.Close
Filename = Dir()
Loop
End Sub
最后,为确保正确保存,请在使用FileFormat
时设置SaveAs
参数。