简体   繁体   中英

Excel: Copying worksheet to another workbook (whether existing or new)

Good day all:

I'm trying to use a ActiveX command button to copy excel worksheets into another file. Here's the background:

I have excel log sheets that are being filled up every day. The logs have a set criterion (A, B, C, etc.) being run daily. While we still want to keep the logs in a daily file, I want a command button to be able to export to another workbook as a master file (eg "A_Masterfile", "B_Masterfile", etc.).

I've tried researching, but all these requirements come from different sites/pages. But since the method they use are so different, I'm having a hard time trying to get all Syntax to fit so that one code can do everything.

As a rundown, here's what I want it to do:

  1. Export active worksheet to another workbook

    a) If workbook exists, copy sheet to end of workbook

    b) If workbook does not exist, create workbook and copy sheet

  2. Destination workbook is based on a cell (criterion A, B, etc.)

  3. Destination workbook might be in a different folder as source worksheet/workbook

Based on what I'm researching so far, this is what I'm turning up with.

When simply copying, this is what I read, but I could not get it to work.

ActiveSheet.Copy After:=Workbooks("Destination.xlsx").Worksheets(Worksheets.Count)

For Creating New File, this is what I read, but even from the original site, they said the problem was it copies the whole workbook, not just one specific sheet.

ActiveWorkbook.SaveAs "C:\path\Destination.xlsx"

Finally, I read about concatenation to create "Destination" file name based on a cell value. However, I got so lost with all the syntax. I tried simply copy pasting but I couldn't get it to work.

This is quite a bit to ask. Thanks so much in advance for all your help! Please let me know if I can clarify anything.

PS Extra note: I've done some QBasic and MATLAB and a tiny bit of JAVA programming in school, so I got the logic part down. But I am quite new to VBA syntax, so extra information would be appreciated. :)

Update: I just learned about "Record Macro" and I tried using it

I got this from it and it works:

Sheets("SourceSheet").Select
ActiveSheet.CheckBoxes.Add(639, 30, 58.8, 16.8).Select
ActiveSheet.CheckBoxes.Add(639.6, 44.4, 58.8, 16.8).Select
ActiveSheet.CheckBoxes.Add(639.6, 61.2, 58.8, 16.8).Select
ActiveSheet.OptionButtons.Add(1279.8, 37.8, 20.4, 18).Select
ActiveSheet.OptionButtons.Add(1280.4, 57, 21.6, 17.4).Select
Sheets("SourceSheet").Copy After:=Workbooks("DestinationMasterFile.xlsx").Sheets(1)
Windows("SourceWorkBook.xlsm").Activate

It works, but only put it after the first sheet instead of putting it in the end. I know it comes from the .Sheets(1), but I don't know how to write it otherwise. Thanks.

I have done a lot more research and trial and error, and I came up with a working code. This might be messy, but it works. Any further improvements are appreciated.

Private Sub CommandButton1_Click()

'Code for Locking
Sheets("W").Unprotect
Range("A1:BZ125").Locked = True
Sheets("W").Protect Password:="hello"






'Code for Copying

'Declarations
Dim Wk As Workbook
Dim FName As String
Dim FNameTwo As String
Dim FilePath As String
Dim TestStr As String
Dim wb As Workbook

'Initializing Constants
Set wb = ThisWorkbook
FName = "C:\Users\PHReyesDa\Desktop\" & Range("BO1") & ".xlsx"
FNameTwo = Range("BO1") + ".xlsx"

'If statement Setup (if exist)
FilePath = FName
    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FilePath)
    On Error GoTo 0

'If statement

If TestStr = "" Then
        'If not existing, create new file
        MsgBox "File didn't exist yet; new file created"
        Set Wk = Workbooks.Add
        Application.DisplayAlerts = False
        Wk.SaveAs Filename:=FName
        Application.DisplayAlerts = True
        Workbooks(FNameTwo).Close SaveChanges:=True
End If

'Reopens Master File
Workbooks.Open FName

wb.Activate

'Find number of worksheets in destination workbook to worksheet could be copied to end of workbook
Dim Num As Integer
Num = Workbooks(FNameTwo).Worksheets.Count

'Copy source worksheet to (the end of) destination workbook
Sheets("W").Select
Sheets("W").Copy After:=Workbooks(FNameTwo).Worksheets(Num)

'Close and save new workbook, confirmation of successful copy
Workbooks(FNameTwo).Close SaveChanges:=True
MsgBox "Worksheet successfully exported and saved to master file"

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