我要打开* .asc文件,重新格式化,然后保存为与原始文件同名的Excel文件(使用xls扩展名)。
我使用宏记录器和我在网上找到的代码打开单个文件并根据需要重新格式化。那部分代码有效。
我无法保存为Excel文件。它给了我Run Time error of 1004 Method ‘SaveAs’ of object ‘_Workbook’ failed
。我尝试了很多不同的代码,我在网上找到了(仍然在那里,只是注释掉了),但都没有用。
两个问题:
您能提供修复SaveAs问题的建议吗?
您能否提供有关如何自动打开和保存一个文件夹中所有文件的建议?
这是我的代码:
Sub OpenFormatSave()
'
' OpenFormatSave Macro
'
Dim StrFileName As String
Dim NewStrFileName As String
ChDir _
"C:\Users\Owner\Documents\work_LLRS\GoM\NASA_data\Satellite_files_GoM_3Dec2012"
StrFileName = Application.GetOpenFilename("NASA Files (*.asc), *.asc")
If TypeName(StrFileName) <> "Boolean" Then
Workbooks.OpenText Filename:=StrFileName, _
Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _
Array(9, 1), Array(10, 1)), TrailingMinusNumbers:=True
End If
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "Year"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Day_of_Year"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Longitude"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Latitude"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Chla_mg_m-3"
Range("F1").Select
ActiveCell.FormulaR1C1 = "POC_mmolC_m-3"
Range("G1").Select
ActiveCell.FormulaR1C1 = "SPM_g_m-3"
Range("H1").Select
ActiveCell.FormulaR1C1 = "aCDOM355_m-1"
Range("I1").Select
ActiveCell.FormulaR1C1 = "DOC_mmolC_m-3"
Range("J1").Select
ActiveCell.FormulaR1C1 = "L2_flags"
Columns("A:B").Select
Selection.NumberFormat = "0"
Columns("C:D").Select
Selection.NumberFormat = "0.0000"
Columns("E:E").Select
Selection.NumberFormat = "0.000"
Columns("F:F").Select
Selection.NumberFormat = "0.0"
Columns("G:H").Select
Selection.NumberFormat = "0.000"
Columns("I:I").Select
Selection.NumberFormat = "0.0"
Columns("J:J").Select
Selection.NumberFormat = "0.00E+00"
'Mid(StrFileName, 1, InStrRev(StrFileName, ".")) = "xlsm"
'With ActiveWorkbook
'NewStrFileName = Replace(.StrFileName, ".asc", ".xls")
' .SaveAs Filename:=FullName, FileFormat:=xlsx, AddToMRU:=False
' .Close SaveChanges:=True
'End With
StrFileName = ThisWorkbook.Name
GetName:
StrFileName = Application.GetSaveAsFilename(NewStrFileName, _
fileFilter:="(*.xlsm), *.xlsm, (*.xlsx), *.xlsx,(*.xls), *.xls")
' FileMonth is the Workbook name, filter options to save a older version file
'If Dir(NewStrFileName) = "" Then
' ActiveWorkbook.SaveAs NewStrFileName
'Else
' If MsgBox("That file exists. Overwrite?", vbYesNo) = vbNo Then GoTo GetName
' Application.DisplayAlerts = False
' ActiveWorkbook.SaveAs Filename:=NewStrFileName, FileFormat:=xlsx, AddToMRU:=False
'Application.DisplayAlerts = True
'End If
'ActiveWorkbook.Close SaveChanges:=True
ActiveWorkbook.SaveAs Filename:=NewStrFileName, FileFormat:=xlsx, CreateBackup:=False
'With ThisWorkbook
'FullName = Replace(.StrFileName, ".asc", ".xlsx")
'.Save
'.SaveAs StrFileName, FileFormat:=xlsx
'.Close
'SaveChanges:=True
'End With
'StrFileName = Split(ActiveWorkbook.FullName, ".xls")(0)
'ActiveWorkbook.SaveAs Filename:="...", FileFormat:=xlsx, AddToMRU:=False
'ActiveWorkbook.Close SaveChanges:=True
'ActiveWorkbook.Save
End Sub
答案 0 :(得分:3)
将SaveAs方法的FileFormat部分更改为:
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
答案 1 :(得分:0)
要遍历文件夹中的所有文件,您有两种选择。
我将举一个Dir的例子,因为它不需要在你的VBA项目中添加引用。不幸的是,Dir界面比FileSystemObject更不直观,也不那么现代。
Dim path As String
path = Dir("C:\Users\example\Documents\AscFiles\*.asc")
Do
If path = vbNullString Then Exit Do
' do something with path here
Debug.Print path
path = Dir
Loop
答案 2 :(得分:0)
您有两个变量StrFileName
(可能是当前文件名)和NewStrFileName
(可能是新文件名)。
在这段代码中:
StrFileName = Application.GetSaveAsFilename(NewStrFileName, _
fileFilter:="(*.xlsm), *.xlsm, (*.xlsx), *.xlsx,(*.xls), *.xls")
你以错误的方式使用这些变量。 “另存为”对话框打开时建议的文件名基于NewStrFileName
,但从未给出过值,因此为空字符串""
。然后,用户选择的值将保存到StrFileName
当您使用以下代码保存文件时:
ActiveWorkbook.SaveAs Filename:=NewStrFileName, FileFormat:=xlsx, _
CreateBackup:=False
NewStrFileName
变量仍然包含""
,因此您尝试保存文件而不给它一个显然会产生错误的名称。
对于一个简单的修复,只需将调用中的两个变量交换为GetSaveAsFilename
:
NewStrFileName = Application.GetSaveAsFilename(StrFileName, _
fileFilter:="(*.xlsm), *.xlsm, (*.xlsx), *.xlsx,(*.xls), *.xls")
这不是达到你想要的最佳方式,但它至少应该起作用