Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 27

Thread: Solved: Button to save selected sheets to new workbook

  1. #1
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location

    Solved: Button to save selected sheets to new workbook

    I have 10 sheets in a workbook. What I wanted to do is have 1 commandbutton to copy sheet1,sheet2,sheet3 then save it as new workbook. Pls
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  2. #2
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    Code below works BUT how can I copy sheets as values only without formula?

    [vba]
    Private Sub cmdOK_Click()
    Dim wks As Worksheet
    Dim strSheet() As String
    Dim strNewWB As String
    Dim strpath As String
    Dim blnFound As Boolean
    Dim intWS As Integer
    Dim i As Integer

    strpath = Application.DefaultFilePath


    For i = 0 To lstDept.ListCount - 1
    If lstDept.Selected(i) = False Then
    ReDim Preserve strSheet(0 To intWS) As String
    strSheet(intWS) = lstDept.List(i)

    intWS = intWS + 1

    Else
    If strNewWB = "" Then
    strNewWB = lstDept.List(i)
    End If
    End If
    Next i

    If UBound(strSheet) = lstDept.ListCount - 1 Then
    MsgBox "Please select one or more departments", vbInformation, _
    "No Selection Made"
    Else
    ActiveWorkbook.SaveAs strpath & "\" & strNewWB & ".xls"

    Application.DisplayAlerts = False
    Sheets(strSheet()).Delete
    Application.DisplayAlerts = True

    ActiveWorkbook.Save

    Unload Me
    End If
    End Sub[/vba]

    [vba]
    Private Sub UserForm_Initialize()
    Dim wks As Worksheet


    For Each wks In ActiveWorkbook.Worksheets
    lstDept.AddItem wks.Name
    Next wks
    End Sub[/vba]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  3. #3
    [VBA]
    ActiveSheet.UsedRange = ActiveSheet.UsedRange.Cells.Value
    [/VBA]
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  4. #4
    Once you have your new workbooks with the 3 copied sheets in it run this code over each copy,it seems to work here and removes the formulas, simply by selecting the sheet copying it and pasting it over the sheet again as a past special values only.

    a bit messy but simple enough, it may help

    cheers

    [vba]
    Cells.Select
    Selection.Copy
    Cells.Select
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    [/vba]

  5. #5
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    So where can I place this code now?

    Quote Originally Posted by YellowLabPro
    [vba]
    ActiveSheet.UsedRange = ActiveSheet.UsedRange.Cells.Value
    [/vba]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    An alternative way

    [vba]

    ActiveWorkbook.SaveCopyAs "C:\Test\new file.xls"
    Set wb = Workbooks.Open("new file.xls")
    Application.DisplayAlerts = False
    wb.Sheets(Array("Sheet10", "Sheet9", "Sheet8", "Sheet7", "Sheet6", "Sheet5", "Sheet4")). _
    Delete
    wb.Save
    Application.DisplayAlerts = True
    [/vba]
    Last edited by Bob Phillips; 08-18-2007 at 02:54 AM.
    ____________________________________________
    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

  7. #7
    Try it here: But do it on a test copy. As I am not sure where this Private Sub Resides. You basically need to place it right after where your code copies the sheet to the other workbook and that sheet is still active.

    [VBA]
    Private Sub UserForm_Initialize()
    Dim wks As Worksheet


    For Each wks In ActiveWorkbook.Worksheets
    lstDept.AddItem wks.Name
    ActiveSheet.UsedRange = ActiveSheet.UsedRange.Cells.Value
    Next wks
    End Sub
    [/VBA]
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  8. #8
    xld-
    Falselse?
    One detail- I don't see in your code where it will return the sheets w/ values instead of formulae.
    That is an elegant way to make a copy, nice..... :-)
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Missed that bit Doug, it was in the follow-up

    [vba]

    ActiveWorkbook.SaveCopyAs "C:\Test\new file.xls"
    Set WB = Workbooks.Open("new file.xls")
    Application.DisplayAlerts = False
    WB.Sheets(Array("Sheet10", "Sheet9", "Sheet8", "Sheet7", "Sheet6", "Sheet5", "Sheet4")). _
    Delete
    For Each sh In WB
    sh.Cells.Value = sh.Cells.Value
    Next sh
    WB.Save
    Application.DisplayAlerts = True
    [/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

  10. #10
    jammer- there you have it.... xld's code cuts out any un-needed steps.

    If you do try mine, let me know if it worked. I know the one line works, just not sure where your code was looping to copy the sheets to the new workbook, and this is where I would drop in the line I gave you. I opted to use UsedRange.Cells as this will not process the entire sheet which will take longer to go through every cell. But UsedRange has some quirks I have read and so you might experience some results you may not desire. So test both ways..... xld's ensure's against this since it processes all the cells.
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  11. #11
    Bob,
    Legal?
    [vba]WB.Sheets Not in (Array("Sheet1", "Sheet2", "Sheet3")).Delete [/vba]
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Afraid not, you would have to loope and MATCH them.
    ____________________________________________
    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

  13. #13
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    This is a quick alternative BUT please note that sheet1 is looking up values to one of this sheet that will be deleted so once the sheet is gone, sheet1 value will turn to #REF!...

    Quote Originally Posted by xld
    Missed that bit Doug, it was in the follow-up

    [vba]

    ActiveWorkbook.SaveCopyAs "C:\Test\new file.xls"
    Set WB = Workbooks.Open("new file.xls")
    Application.DisplayAlerts = False
    WB.Sheets(Array("Sheet10", "Sheet9", "Sheet8", "Sheet7", "Sheet6", "Sheet5", "Sheet4")). _
    Delete
    For Each sh In WB
    sh.Cells.Value = sh.Cells.Value
    Next sh
    WB.Save
    Application.DisplayAlerts = True
    [/vba]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  14. #14
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    This simple code below answers my query... Thanks to all response...

    [VBA]
    Sheets(Array("Comments", "Audit Summary", "Summary", "Deduction")).Copy
    Set wb = ActiveWorkbook
    wb.SaveAs ThisWorkbook.Path & "\NewWorkbook.xls"[/VBA]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What about values witjout formulae?
    ____________________________________________
    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

  16. #16
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    Well that remain unsolved but I can go on with this code for now. My Boss really wanted me to finished an output ASAP. If you got any solution "sure you have" i will appreciate it...

    Quote Originally Posted by xld
    What about values witjout formulae?
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  17. #17
    jammer,
    You have a new workbook w/ the desired sheets in this workbook now?
    These sheets are the sheets listed in your array-
    "Comments", "Audit Summary", "Summary", "Deduction" ?
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  18. #18
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    Yes I do have as I desired. The only thing now is those sheets listed in my array still have formulas link to the OLD Workbook wherein during opening the newworkbook, it still ask the user if they want to update links or not which I wanted to eliminate.
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  19. #19
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Just tag that bit of code that I added after Doug's comment onto your cde.
    ____________________________________________
    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

  20. #20
    Just to fully understand-
    Which sheet contains the formulas?
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

Posting Permissions

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