我有一个Excel宏从Yahoo下载股票代码信息并将其保存为csv。然后调用一个函数来处理csv,生成一个图表并将生成的xlsm文件存储为html文件。
我想将连续的csv文件传递给函数,处理它们并将生成的xlsm文件存储到原始xlsm文件的新工作表中。
If (Year = 0) Then Application.DisplayAlerts = False ActiveWorkbook.SaveAs Filename:=FilePAth & Ticker & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False ActiveWorkbook.SaveAs Filename:=FilePAth & Ticker & ".htm", FileFormat:=xlHtml, ReadOnlyRecommended:=False, CreateBackup:=False Else ActiveWorkbook.Copy After:=Workbooks(FilePAth & Ticker & ".xlsm").Sheets(Ticker) ActiveWorkbook.SaveAs Filename:=FilePAth & Ticker & ".htm", FileFormat:=xlHtml, ReadOnlyRecommended:=False, CreateBackup:=False Workbooks(Ticker & ".csv").Close SaveChanges:=True End If
上述条件的第一部分创建了xlsm& htm文件,第二部分尝试保存工作表。 在: - ActiveWorkbook.Copy
上发生错误有人可以提供任何线索吗?
提前感谢。
答案 0 :(得分:0)
代码查找Master工作簿。如果它没有找到它,那么它将打开它。使用打开的Master工作簿,工作表可以从一开始移动到另一个工作簿。我创造了一种通用的方式来保存然后关闭。
这是您需要的行。
ThisWorkbook.Sheets(1).Copy After:=Compiled_WB.Sheets(Compiled_WB.Worksheets.Count)
这是一个将所有内容组合在一起的Sub。
Sub tryit()
Application.DisplayAlerts = False
Dim Ticker As String
Ticker = "DocumentsTicker"
Dim year As Long
year = 1
Dim Compiled_WB As Workbook
If (year = 0) Then
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & Ticker & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & Ticker & ".htm",FileFormat:=xlHtml, ReadOnlyRecommended:=False, CreateBackup:=False
Else
For Each Compiled_WB In Workbooks
If InStr(Compiled_WB.Name, Ticker) > 0 Then
GoTo already_open
End If
Next
Workbooks.Open Filename:=ThisWorkbook.Path & Ticker & ".xlsm"
For Each Compiled_WB In Workbooks
If InStr(Compiled_WB.Name, Ticker) > 0 Then
GoTo already_open
End If
Next
already_open:
ThisWorkbook.Sheets(1).Copy After:=Compiled_WB.Sheets(Compiled_WB.Worksheets.Count)
Dim filetype As String
If Compiled_WB.FileFormat = 52 Then
filetype = ".htm"
Else
filetype = ".xlsm"
End If
Compiled_WB.Save
Compiled_WB.SaveAs Filename:=Compiled_WB.Path & "\" & Ticker & filetype, FileFormat:=xlHtml, ReadOnlyRecommended:=False, CreateBackup:=False
Compiled_WB.Close
Workbooks(Ticker & ".csv").Close SaveChanges:=True
End If
End Sub'