[英]VBA copying an Excel worksheet to a new worksheet in another workbook
I have an Excel Macro that downloads stock Ticker information off Yahoo and saves it as a csv. 我有一个Excel宏,可以从Yahoo下载股票行情自动收录器信息,并将其另存为csv。 A function is then called to process the csv, generate a chart and store resulting xlsm file as a html file. 然后调用一个函数来处理csv,生成图表并将生成的xlsm文件存储为html文件。
I want to pass successive csv files into the function, process them and store resulting xlsm file into new worksheets on the original xlsm file. 我想将连续的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
The first part of the above condition creates the xlsm & htm file, the second part attempts to save the worksheet. 上述条件的第一部分将创建xlsm&htm文件,第二部分将尝试保存工作表。 An error occurs on the:- ActiveWorkbook.Copy 在以下位置发生错误:-ActiveWorkbook.Copy
Can anybody provide any clues please? 有人可以提供任何线索吗?
thanks in advance. 提前致谢。
The code looks for the Master workbook. 该代码查找主工作簿。 If it doe not find it, then it will open it. 如果找不到,它将打开它。 With the open Master workbook, the sheet can then move from on to the other. 使用打开的“母版”工作簿,然后可以将工作表从另一个移到另一个。 I created a universal way for it to save then close. 我为它创建了一种保存然后关闭的通用方法。
This is the line you need. 这是您需要的线。
ThisWorkbook.Sheets(1).Copy After:=Compiled_WB.Sheets(Compiled_WB.Worksheets.Count)
Here is a Sub that puts it all together. 这是将所有内容组合在一起的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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.