Consulting

Results 1 to 5 of 5

Thread: Solved: Save Sheet to a user defined location

  1. #1
    Banned VBAX Regular
    Joined
    Mar 2007
    Posts
    13
    Location

    Solved: Save Sheet to a user defined location

    Hi,
    I have a workbook with 6 worksheets, I want to save each sheet to different folder. The folder will be specified by the user at the run time.

  2. #2
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Assuming that you want all sheets saved to the same folder and using the sheet tab name as the new filename,

    [VBA]
    Sub SaveSheets()
    Dim wks As Worksheet
    Dim varFolder As Variant


    ' capture the new file location
    varFolder = Application.GetSaveAsFilename()

    ' only proceed if the user did not cancel
    If varFolder <> False Then
    ' loop thru the sheets
    For Each wks In ActiveWorkbook.Worksheets
    wks.Copy

    ActiveWorkbook.SaveAs Left(varFolder, InStrRev(varFolder, "\")) & wks.Name
    ActiveWorkbook.Close False
    Next wks
    End If
    End Sub

    [/VBA]

  3. #3

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You can use FileDialog to get the folder rather that GetOpenFilename and stripping the filename

    [vba]

    Sub SaveSheets()
    Dim wks As Worksheet
    Dim varFolder As Variant


    ' capture the new file location
    With Application.FileDialog(msoFileDialogFolderPicker)
    If .Show = -1 Then
    varFolder = .SelectedItems(1)

    For Each wks In ActiveWorkbook.Worksheets
    wks.Copy

    ActiveWorkbook.SaveAs varFolder & "\" & wks.Name
    ActiveWorkbook.Close False
    Next wks
    End If
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Banned VBAX Regular
    Joined
    Mar 2007
    Posts
    13
    Location
    That worked

Posting Permissions

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