+ Reply to Thread
Results 1 to 13 of 13

Copy spreadsheet into another file.

  1. #1
    Forum Contributor
    Join Date
    02-16-2006
    Posts
    241

    Copy spreadsheet into another file.

    Looking for code to:

    Copy "Master" spreadsheet to O:\Sales_Tax and rename spreadsheet to Last Month. (For example February, if I executed the code today)

    Thanks,

    Met

  2. #2
    GS
    Guest

    RE: Copy spreadsheet into another file.

    If I understand you correctly, you want to save a copy of the active workbook
    with a filename that represents the previous month. If so, this should do it:

    Sub SaveCopyAs_LastMonth()
    Dim sFileName As String
    sFileName = "O:\Sales_Tax" & MonthName(Month(Date) - 1) & ".xls"
    ActiveWorkbook.SaveCopyAs sFileName
    End Sub

    =OR-
    Did you want to copy the ActiveSheet into a new workbook and save it? If so,
    post back.

    Regards,
    GS

  3. #3
    GS
    Guest

    RE: Copy spreadsheet into another file.

    Oops! I forgot one separator. Change this line:

    sFileName = "O:\Sales_Tax" & MonthName(Month(Date) - 1) & ".xls"

    to:

    sFileName = "O:\Sales_Tax\" & MonthName(Month(Date) - 1) & ".xls"

    Regards,
    GS


    "GS" wrote:

    > If I understand you correctly, you want to save a copy of the active workbook
    > with a filename that represents the previous month. If so, this should do it:
    >
    > Sub SaveCopyAs_LastMonth()
    > Dim sFileName As String
    > sFileName = "O:\Sales_Tax" & MonthName(Month(Date) - 1) & ".xls"
    > ActiveWorkbook.SaveCopyAs sFileName
    > End Sub
    >
    > =OR-
    > Did you want to copy the ActiveSheet into a new workbook and save it? If so,
    > post back.
    >
    > Regards,
    > GS


  4. #4
    Forum Contributor
    Join Date
    02-16-2006
    Posts
    241

    Almost

    I am looking to save it in an exsisting workbook called O:\Sales_Tax.xls


    Thanks,
    Met

  5. #5
    GS
    Guest

    Re: Copy spreadsheet into another file.

    Okay, I understand now. Here's some code that should work:

    Sub MoveSheetsToOtherBook()
    ' Moves the selected sheet(s) to a specified workbook
    ' If the workbook isn't open, it opens it.
    ' If the file doesn't exist, it exits.
    ' Requires bBookIsOpen(), bFileExists functions

    Dim wbkTarget As Workbook, wbkSource As Workbook
    Dim Shts As Sheets

    Const sPath As String = "O:\"
    Const sName As String = "Sales_Tax.xls"

    Set wbkSource = ActiveWorkbook 'or ThisWorkbook

    'If wbkSource has been saved, you can't move all its sheets.
    'If wbkSource was created from a template but not saved yet,
    'delete the following line and its corresponding End If.
    If Not ActiveWindow.SelectedSheets.Count = ActiveWorkbook.Sheets.Count Then
    Set Shts = ActiveWindow.SelectedSheets

    'Get a reference to wbkTarget
    If Not bBookIsOpen(sName) Then
    If bFileExists(sPath & sName) Then
    Set wbkTarget = Workbooks.Open(sPath & sName)
    Else
    MsgBox "The target file does not exist !", vbExclamation + vbOKOnly
    Exit Sub
    End If
    Else
    Set wbkTarget = Workbooks(sName)
    End If

    'Move the sheet(s) to wbkTarget, save & close it.
    wbkSource.Sheets(Shts).Move
    after:=wbkTarget.Sheets(wbkTarget.Sheets.Count)
    With wbkTarget
    .Save
    .Close
    End With
    End If

    End Sub


    Function bBookIsOpen(wbkName) As Boolean
    ' Checks if a specified workbook is open.
    '
    ' Arguments: wbkName The name of the workbook
    '
    ' Returns: True if the workbook is open

    Const sSource As String = "bBookIsOpen()"

    Dim x As Workbook
    On Error Resume Next
    Set x = Workbooks(wbkName)
    bBookIsOpen = (Err = 0)

    End Function


    Function bFileExists(fileName As String) As Boolean
    ' Checks if a file exists in the specified folder
    '
    ' Arguments: fileName The fullname of the file
    '
    ' Returns: TRUE if the file exists

    Const sSource As String = "bFileExists()"

    On Error Resume Next
    bFileExists = (Dir$(fileName) <> "")

    End Function

    '**
    Regards,
    GS

  6. #6
    GS
    Guest

    Re: Copy spreadsheet into another file.

    Oops! You did say copy, ..not move. -Sorry about not catching that!
    Replace MoveSheetsToOtherBook() with this:

    Sub CopySheetToOtherBook()
    ' Copies the active sheet to a specified workbook.
    ' If the workbook isn't open, it opens it.
    ' If the file doesn't exist, it exits.
    ' Requires bBookIsOpen(), bFileExists functions

    Dim wks As Worksheet, wbkTarget As Workbook
    Dim sNewName As String

    Const sPath As String = "O:\"
    Const sName As String = "Sales_Tax.xls"

    Set wks = ActiveSheet
    sNewName = MonthName(Month(Date) - 1)


    'Get a reference to wbkTarget
    If Not bBookIsOpen(sName) Then
    If bFileExists(sPath & sName) Then
    Set wbkTarget = Workbooks.Open(sPath & sName)
    Else
    MsgBox "The target file does not exist !", vbExclamation + vbOKOnly
    Exit Sub
    End If
    Else
    Set wbkTarget = Workbooks(sName)
    End If

    'Move the sheet(s) to wbkTarget, save & close it.
    wks.Copy after:=wbkTarget.Sheets(wbkTarget.Sheets.Count)
    With wbkTarget
    ActiveSheet.Name = sNewName
    .Save
    .Close
    End With

    End Sub


    Regards,
    GS

  7. #7
    Forum Contributor
    Join Date
    02-16-2006
    Posts
    241

    Requires bBookIsOpen()

    Can you explain this? Is this a seperate sub/module? Please advise.

    Thanks,

    MET

  8. #8
    GS
    Guest

    Re: Copy spreadsheet into another file.

    Hi Metrazol,

    My previous post is a sub, and should be placed in a standard module.
    Disregard all previous examples because the last one copies the active sheet
    ("Master") into your existing file. The comments at the beginning of the
    procedure explain what it does, while each process within the procedure has
    comment that outline the steps.

    Just call the procedure, or assign it to a button. Make sure the "Master"
    sheet you want to copy into the other workbook is active. (the one you're
    working on)

    Regards,
    GS


    "Metrazal" wrote:

    >
    > Can you explain this? Is this a seperate sub/module? Please advise.
    >
    > Thanks,
    >
    > MET
    >
    >
    > --
    > Metrazal
    > ------------------------------------------------------------------------
    > Metrazal's Profile: http://www.excelforum.com/member.php...o&userid=31648
    > View this thread: http://www.excelforum.com/showthread...hreadid=518833
    >
    >


  9. #9
    Forum Contributor
    Join Date
    02-16-2006
    Posts
    241

    Compile Error

    When I input the sub as entered and run it, I get the following error:

    Compile Error
    Sub or Function not defined

    * bBookIsOpen is highlighted


    Thanks,
    Met

  10. #10
    GS
    Guest

    Re: Copy spreadsheet into another file.

    Hi Met,

    As the lead comments in the sub indicate, you still need to use the two
    support functions in my other post. Here they are again:

    Function bBookIsOpen(wbkName) As Boolean
    ' Checks if a specified workbook is open.
    '
    ' Arguments: wbkName The name of the workbook
    '
    ' Returns: True if the workbook is open

    Const sSource As String = "bBookIsOpen()"

    Dim x As Workbook
    On Error Resume Next
    Set x = Workbooks(wbkName)
    bBookIsOpen = (Err = 0)

    End Function


    Function bFileExists(fileName As String) As Boolean
    ' Checks if a file exists in the specified folder
    '
    ' Arguments: fileName The fullname of the file
    '
    ' Returns: TRUE if the file exists

    Const sSource As String = "bFileExists()"

    On Error Resume Next
    bFileExists = (Dir$(fileName) <> "")

    End Function

    Regards,
    Garry

  11. #11
    Forum Contributor
    Join Date
    02-16-2006
    Posts
    241

    Got it..

    Done deal...

    Thanks..

    One more thing:

    I need the date to be in the format FEB 2006, I still need the date to back up one month but abbreviate the date and display the year.

    Once again..

    Thanks,
    Met

  12. #12
    GS
    Guest

    Re: Copy spreadsheet into another file.

    Hi Met,

    Modify as follows:

    sNewName = UCase$(left(monthname(month(date)-1),3)) & " " & year(date)

    Regards,
    Garry


    "Metrazal" wrote:

    >
    > Done deal...
    >
    > Thanks..
    >
    > One more thing:
    >
    > I need the date to be in the format FEB 2006, I still need the date
    > to back up one month but abbreviate the date and display the year.
    >
    > Once again..
    >
    > Thanks,
    > Met
    >
    >
    > --
    > Metrazal
    > ------------------------------------------------------------------------
    > Metrazal's Profile: http://www.excelforum.com/member.php...o&userid=31648
    > View this thread: http://www.excelforum.com/showthread...hreadid=518833
    >
    >


  13. #13
    Forum Contributor
    Join Date
    02-16-2006
    Posts
    241

    Smile Thats it..

    Once again..

    Many thanks..

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1