Results 1 to 6 of 6

Thread: [RESOLVED] Excel: Move worksheet to new book and save as using dialog box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Resolved [RESOLVED] Excel: Move worksheet to new book and save as using dialog box

    I have an excel file with VBA code that generates a summary report, then moves the summary report into a new book and then saves that report into a pre-determined folder. The code I use to do this is:
    Code:
        'Save summary sheet to a new work book
    
        compile_date = Format(Now, "d mmm yy")
        name_of_file = "Report " & compile_date
    
        Dim w As Workbook, ws As Worksheet, ss As Worksheet
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name = "summary" Then
                If w Is Nothing Then
                    ws.Move
                    Set w = ActiveWorkbook
                Else
                    ws.Move after:=ss
                End If
                 Set Wb = ActiveWorkbook
                Set ss = ActiveSheet
            End If
        Next ws
    
        ThisWorkbook.Activate
            
    Wb.SaveAs Filename:= _
          "G:\My Branch \Dragon Files\” & name_of_file & ".xls"
    This works fine for me. (I’ve simplified the code a bit for illustration purposes: the complete version chooses the folder based upon the date.) The file path is in a group drive. I would like to send my report generator to someone else in my organisation. She belongs to a different branch and I don’t know how she’s set up the folders in her G drive. So I want to rewrite the code so that the MS Excel Save As dialogue box comes up (preferably with the name of the file in the File Name field), so that she can choose where she wants to save it.
    To date, my attempts to do so have resulted in a Save As dialogue box that tries to save the file that generates the report and not the report itself.
    So, how do I go about this?
    Thank you

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel: Move worksheet to new book and save as using dialog box

    Something like this?

    Code:
    Sub YourMacro()
        Dim fileSaveName As String
    
        '~~> Your other codeOTHER CODE
        
        fileSaveName = Application.GetSaveAsFilename( _
        fileFilter:="xls Files (*.xls), *.xls")
    
        If fileSaveName <> "False" Then
            ActiveWorkbook.SaveAs fileSaveName
        End If
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Re: Excel: Move worksheet to new book and save as using dialog box

    I F8-ed through it all. The problem is that the code I was using to move the sheet set the focus back to the report generator file, instead of going to the new file. With a "GoTo" inserted in the code, it is now doing what I want.

    Code:
      Sub ReportSaver()
       
        Dim fileSaveName As String
    
        'Save summary sheet to a new work book
    
        compile_date = Format(Now, "d mmm yy")    '  name_of_file
     fileSaveName = "Report " & compile_date
    
        Dim w As Workbook, ws As Worksheet, ss As Worksheet
        For Each ws In ThisWorkbook.Worksheets
            If ws.Name = "summary" Then
                If w Is Nothing Then
                    ws.Move
                    Set w = ActiveWorkbook
       GoTo SaveItAs
                Else
                    ws.Move after:=ss
    
                End If
                 Set Wb = ActiveWorkbook
                Set ss = ActiveSheet
            End If
        Next ws
    
        ThisWorkbook.Activate
    
    
        '~~> Your other codeOTHER CODE
    SaveItAs:
        
        fileSaveName = Application.GetSaveAsFilename( _
        fileFilter:="xls Files (*.xls), *.xls")
    
        If fileSaveName <> "False" Then
            ActiveWorkbook.SaveAs fileSaveName
        End If
    
    
       End Sub
    So this one is now resolved! Thank you Koolsid, I think of you as my VBA mentor.

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [RESOLVED] Excel: Move worksheet to new book and save as using dialog box

    Though the problem is solved but I am not too happy with the code...

    What exactly are you trying to do? Explain the steps one by one

    Sid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Re: [RESOLVED] Excel: Move worksheet to new book and save as using dialog box

    Dear Sid

    My code lives in an excel 2003 file called “Report Generator.xls”. The code in this file takes data from various exports from a mainframe system and puts them into a summary format. With the code I wrote for my own use it then saves the summary into a pre-determined folder in my G drive.

    Because my colleague won’t have the same G drive structure as I have, I want to amend the code so that she can save the summary using the “save as” that MS Excel provides – as stated earlier, ideally, with the file name already sitting in the dialogue box. (The code I used for moving the summary sheet is code that I “borrowed” from elsewhere .)
    Agreed, the code looks a bit “clunky” – but it does do the job.

    Cheers

    The Dragon

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [RESOLVED] Excel: Move worksheet to new book and save as using dialog box

    Fair Enough
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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