简体   繁体   中英

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. A function is then called to process the csv, generate a chart and store resulting xlsm file as a html file.

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.

  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. An error occurs on the:- 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 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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM