Consulting

Results 1 to 10 of 10

Thread: Solved: Save only one sheet

  1. #1
    VBAX Regular Gert Jan's Avatar
    Joined
    Oct 2006
    Location
    Houten
    Posts
    62
    Location

    Solved: Save only one sheet

    Hi everyone
    I have a userform from wich the data, entered by users is stored in sheet(1).
    I would like to have a commandbutton on the form, to copy only sheet(1) in a new workbbook
    and save this workbook with just this one sheet to a path specified in sheet(2) cell A2.
    The name of the saved workbook has to be a combination of the text in
    sheet(1)cell C10 and sheet(1)cell B5 separated by a underscore.

    Can anyone help me to get started with the code for the button please?

    Gert Jan

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim sPath As String

    sPath = ActiveWorkbook.Worksheets("Sheet2").Range("A2").Value
    Worksheets("Sheet1").Move
    With ActiveWorkbook
    .SaveAs Filename = sPath & "\" & .Range("C10").Value & "_" & Range("B5").Value & ".xls"
    End With
    [/vba]

  3. #3
    VBAX Regular Gert Jan's Avatar
    Joined
    Oct 2006
    Location
    Houten
    Posts
    62
    Location
    Thanks for your quick resonse Bob,
    when i run this i get an error, 438, this method is not supported by this object.
    At first try, it said that Filename is not defined, so i've put in "Dim Filename as String", could this be causing the problem?

    Gert Jan

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Dim sPath As String

    sPath = ActiveWorkbook.Worksheets("Sheet2").Range("A2").Value
    Worksheets("Sheet1").Copy
    With ActiveWorkbook.Sheets(1)
    .SaveAs Filename:=sPath & "\" & .Range("C10") & "_" & .Range("B5") & ".xls"
    End With

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular Gert Jan's Avatar
    Joined
    Oct 2006
    Location
    Houten
    Posts
    62
    Location
    I already did change move into copy because the sheet has to stay in the original workbook as well.
    Also, i added a worbook.close, so after saving the new workbook closes.
    [vba]Dim sPath As String
    sPath = ActiveWorkbook.Worksheets("Blad2").Range("A2").Value
    Worksheets("Formulier").Copy
    With ActiveWorkbook.Sheets(1)
    .SaveAs Filename:=sPath & "\" & .Range("C10") & "_" & .Range("B5") & ".xls"
    End With
    ActiveWorkbook.Close
    End Sub[/vba]

    Bob, Malcolm, thank you both for helping

    Gert Jan

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by xld
    [vba]

    Dim sPath As String

    sPath = ActiveWorkbook.Worksheets("Sheet2").Range("A2").Value
    Worksheets("Sheet1").Move
    With ActiveWorkbook
    .SaveAs Filename = sPath & "\" & .Range("C10").Value & "_" & Range("B5").Value & ".xls"
    End With
    [/vba]
    My bad. It should be

    Filename:=

    not

    Filename =

    and it needs sheet references as well as book, as in Malcolm's example.

  7. #7
    VBAX Regular Gert Jan's Avatar
    Joined
    Oct 2006
    Location
    Houten
    Posts
    62
    Location
    Quote Originally Posted by xld
    My bad.
    I won't go tell anybody if you won't

    Thanks again Bob,

    Gert Jan

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Gert Jan
    I won't go tell anybody if you won't

    Thanks again Bob,

    Gert Jan
    But how do we silence Malcolm?

  9. #9
    VBAX Regular Gert Jan's Avatar
    Joined
    Oct 2006
    Location
    Houten
    Posts
    62
    Location
    Quote Originally Posted by xld
    But how do we silence Malcolm?
    That would be like turning lead into gold.........impossible

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    My silence can be bought for a very reasonable amount of Gold!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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