Consulting

Results 1 to 10 of 10

Thread: help save worksheet to new wb

  1. #1
    VBAX Contributor
    Joined
    Jun 2008
    Posts
    169
    Location

    help save worksheet to new wb

    i searched the thread find the code but can't ok as i use excel 2007

    Sub savesheet2()
    Application.ScreenUpdating = False
    ActiveSheet.Select
    ActiveSheet.Copy
    ThisFile = "PcikupExp" & Format(Date, "ddmmyy" & "hh:mm") & ".xlsx"
    ActiveSheet.SaveAs Filename:="D:\Exp"
    Application.ScreenUpdating = True
    ActiveWorkbook.Close
    End Sub

    pls help do any part i should change or pls help

  2. #2
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    Try the following. The ":" is a restricted charater for a filename so is changed to "-" and I changed your file location for my testing.

    [vba]Sub savesheet()
    Dim Original_Workbooks As Integer
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Original_Workbooks = Workbooks.Count
    ThisFile = "PickupExp" & Replace(Format(Now(), "ddmmyy" & "hh:mm") & ".xlsx", ":", "-")
    ActiveSheet.Copy
    With Workbooks(Original_Workbooks + 1)
    .SaveAs Filename:="C:\Test\" & ThisFile
    .Close
    End With
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub[/vba]
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  3. #3
    VBAX Contributor
    Joined
    Jun 2008
    Posts
    169
    Location
    Quote Originally Posted by Teeroy
    Try the following. The ":" is a restricted charater for a filename so is changed to "-" and I changed your file location for my testing.

    [vba]Sub savesheet()
    Dim Original_Workbooks As Integer
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Original_Workbooks = Workbooks.Count
    ThisFile = "PickupExp" & Replace(Format(Now(), "ddmmyy" & "hh:mm") & ".xlsx", ":", "-")
    ActiveSheet.Copy
    With Workbooks(Original_Workbooks + 1)
    .SaveAs Filename:="C:\Test\" & ThisFile
    .Close
    End With
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub[/vba]

    Teeroy , thank very your help,
    the code work with Run Time Error "1004"
    Message " The file could not be accessed, try one of the following:

    I do sure the file / folder haven't the symbol characters


  4. #4
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    It looks like the file coudn't be saved at the location specified. Did you change the file location? The line
    [vba].SaveAs Filename:="C:\Test\" & ThisFile [/vba]
    should change to
    [vba].SaveAs Filename:="D:\Exp\" & ThisFile [/vba]
    to save in the folder "D:\Exp\".
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  5. #5
    VBAX Contributor
    Joined
    Jun 2008
    Posts
    169
    Location
    Quote Originally Posted by Teeroy
    It looks like the file coudn't be saved at the location specified. Did you change the file location? The line
    [vba].SaveAs Filename:="C:\Test\" & ThisFile [/vba]
    should change to
    [vba].SaveAs Filename:="D:\Exp\" & ThisFile [/vba]
    to save in the folder "D:\Exp\".

    Changed the path, but same the error issue !!
    highlighted
    .SaveAs Filename:="D:\Exp\" & ThisFile

  6. #6
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    Can you please copy and post the exact code you have in the workbook?
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  7. #7
    VBAX Contributor
    Joined
    Jun 2008
    Posts
    169
    Location
    Quote Originally Posted by Teeroy
    Can you please copy and post the exact code you have in the workbook?
    here's exactly your code :
    [VBA]Sub savesheet()
    Dim Original_Workbooks As Integer
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Original_Workbooks = Workbooks.Count
    ThisFile = "PickupExp" & Replace(Format(Now(), "ddmmyy" & "hh:mm") & ".xlsx", "-", ":")
    ActiveSheet.Copy
    With Workbooks(Original_Workbooks + 1)
    .SaveAs fileName:="D:\Exp\" & ThisFile
    .Close
    End With
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub[/VBA]

  8. #8
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    OK, if you have the default save set as .XLS or you start from a .XLS file you might need the file format specified. Change the .SaveAs line to

    [vba].SaveAs fileName:="D:\Exp\" & ThisFile FileFormat:=51[/vba]
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  9. #9
    VBAX Contributor
    Joined
    Jun 2008
    Posts
    169
    Location
    Quote Originally Posted by Teeroy
    OK, if you have the default save set as .XLS or you start from a .XLS file you might need the file format specified. Change the .SaveAs line to

    [vba].SaveAs fileName:="D:\Exp\" & ThisFile FileFormat:=51[/vba]


    yeah ... its work great
    thanks very much your help .

  10. #10
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    You are welcome.
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

Posting Permissions

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