Results 1 to 3 of 3

Thread: help with save as function....please!!

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    10

    help with save as function....please!!

    I have an excel with multiple worksheets, on each sheet I want to place a button that, when pressed, saves that sheet to a new file. Anyone know how to do this, using saveas, so that the user can input where the file is saved and what the name is.


    Thanks!

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: help with save as function....please!!

    I dont think that the SaveAs function will allow you to save an individual worksheet into a new workbook, however it can be done with a bit of fiddling around.

    Paste the Code into a module in your workbook!

    The following example assumes that you have a Button called "commandbutton1" & a textbox called "TxtFileName" on your worksheet.

    The Code below first takes a copy of the activeworksheet and then pastes it into a new workbook.

    Then it loops through the activeworkbooks and trys to find any newly created, i.e - any Workbooks name "Book.." something.

    The first one it finds it Saves it with the filename entered by the user in the txtbox & closes it.

    Obviously this does rely on you not having any other unsaved workbooks open while you are running it, or any saved workbooks open who's names start with "Book" !!!.

    Maybe you could modify it to work better !!!

    VB Code:
    1. Sub CommandButton1_Click()
    2.  
    3. If Len(TxtFileName) > 0 Then
    4.     SaveSheetToNewWorkbook
    5. End If
    6.  
    7. End Sub
    8. Sub SaveSheetToNewWorkbook()
    9.  
    10. Dim n As Integer
    11. Dim blnsave As Boolean
    12. Dim strSavePath As String
    13. Dim strFileName As String
    14.  
    15. strSavePath = "C:\Temp\"
    16. strFileName = TxtFileName
    17.  
    18. With Application
    19.     .ActiveSheet.Select
    20.     .ActiveSheet.Copy          ' - Copies the worksheet into a new workbook
    21.     '.ActiveSheet.Move         ' - Moves the worksheet into a new workbook
    22.    
    23.     For n = 1 To .Workbooks.Count
    24.         If Left(.Workbooks.Item(n).Name, 4) = "Book" Then
    25.             .Workbooks.Item(.Workbooks.Item(n).Name).SaveAs (strSavePath & strFileName)
    26.             .Workbooks.Item(n).Close
    27.         End If
    28.     Next n
    29. End With
    30.  
    31. End Sub

  3. #3
    New Member
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    7

    Re: help with save as function....please!!

    instead of a text box for inputting the new filename, you could use the GetSaveAsFilename method.

Posting Permissions

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



Click Here to Expand Forum to Full Width