Results 1 to 10 of 10

Thread: Save as button for Excel

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    54

    Save as button for Excel

    Whoops sorry ignore this one.
    Last edited by jjchad; Jul 27th, 2005 at 02:15 AM.

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Save as button for Excel

    John,

    If you set the limits on the shifts within the code, you can keep this down to just one button..

    VB Code:
    1. Dim TimeNow as Date
    2.   TimeNow = Now
    3.   If TimeNow <= CDate(#12:00:00 PM#) And TimeNow >= CDate(#6:00:00 AM#) Then
    4.     ActiveWorkbook.SaveAs "Morning File"
    5.   ElseIf TimeNow >= CDate(#12:00:01 PM#) And TimeNow <= CDate(#6:00:00 PM#) Then
    6.     Activeworkbook.SaveAs "Afternoon File"
    7.   ElseIf TimeNow >= CDate(#6:00:01 PM#) And TimeNow <= CDate(#12:00:00 AM#) Then
    8.     ActiveWorkbook.SaveAs "Evening File"
    9.   Else
    10.     ActiveWorkbook.SaveAs "Night File"
    11.   End If
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    54

    Re: Save as button for Excel

    Thanks Danny i'll give it a go.

    Sounds like just the code i need.

    John

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    54

    Re: Save as button for Excel

    Just one question Danny what code should i use to specify where to save the file.?


    John
    Last edited by jjchad; Jul 27th, 2005 at 02:38 AM.

  5. #5
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Save as button for Excel

    John..

    This one..

    ActiveWorkbook.SaveAs "Morning File"

    Replace the "Morning File" with the pathname and filename you want to save it.

    If you need to save the file as a dynamic filename then capture the filename above the If statement and then use it like so

    Code:
      Dim FileName As String
      FileName = "FileDated" & Format(Date,"mmddyyyy") 'Or you localised setting
      Dim TimeNow as Date
      TimeNow = Now
      If TimeNow <= CDate(#12:00:00 PM#) And TimeNow >= CDate(#6:00:00 AM#) Then
        ActiveWorkbook.Save As "C:\MorningFiles\" & FileName
      ElseIf ...
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    54

    Re: Save as button for Excel

    Hi Danny i'm not sure i'm doint this right, this is my code so far:

    VB Code:
    1. Dim TimeNow As Date
    2.   TimeNow = Now
    3.  
    4.  If TimeNow >= CDate(#6:00:00 AM#) And TimeNow <= CDate(#2:00:00 PM#) Then
    5.     ActiveWorkbook.SaveAs "Mornings"
    6.   ElseIf TimeNow >= CDate(#2:00:01 PM#) And TimeNow <= CDate(#10:00:00 PM#) Then
    7.     ActiveWorkbook.SaveAs "Afternoons"
    8.   ElseIf TimeNow >= CDate(#10:00:01 PM#) And TimeNow <= CDate(#6:00:00 AM#) Then
    9.     ActiveWorkbook.SaveAs "Nights"
    10.   End If

    It's not saving the file thou any idea's why.?


    John

  7. #7
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Save as button for Excel

    It wont do as

    "Mornings" is not a valid path for a file to be saved.. this will need to be something like "C:\Mornings\" &filename

    check Post #5
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    54

    Re: Save as button for Excel

    Danny i've tried this too but no luck.?

    VB Code:
    1. Dim TimeNow As Date
    2. Dim FileName As String
    3.   TimeNow = Now
    4.  
    5.  FileName = "FileDated" & Format(Date, "mmddyyyy") 'Or you localised setting
    6.  
    7.  If TimeNow <= CDate(#6:00:00 AM#) And TimeNow >= CDate(#2:00:00 PM#) Then
    8.     ActiveWorkbook.SaveAs "C:\Documents and Settings\John Chadwick\My Documents\Andon\Mornings" & FileName
    9.  
    10.   ElseIf TimeNow >= CDate(#2:00:01 PM#) And TimeNow <= CDate(#10:00:00 PM#) Then
    11.     ActiveWorkbook.SaveAs "C:\Documents and Settings\John Chadwick\My Documents\Andon\Afternoons" & FileName
    12.  
    13.   ElseIf TimeNow >= CDate(#10:00:01 PM#) And TimeNow <= CDate(#6:00:00 AM#) Then
    14.     ActiveWorkbook.SaveAs "C:\Documents and Settings\John Chadwick\My Documents\Andon\Nights" & FileName
    15.   End If

    John

  9. #9
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Save as button for Excel

    Sorry My Bad..

    You need to add the extension on the filename..

    Your code is also slightly wrong.. The first if statement's between statement is stopping the first file from being create switch it to this

    VB Code:
    1. Dim TimeNow As Date
    2.   Dim FileName As String
    3.   TimeNow = Format(Now, "HH:NN:SS")
    4.  
    5.   FileName = "FileDated" & Format(Date, "mmddyyyy") & ".xls" 'Or you localised setting
    6.  
    7.   If TimeNow >= CDate(#6:00:00 AM#) And TimeNow <= CDate(#2:00:00 PM#) Then
    8.     ActiveWorkbook.SaveAs "C:\" & FileName
    9.   ElseIf TimeNow >= CDate(#2:00:01 PM#) And TimeNow <= CDate(#10:00:00 PM#) Then
    10.     ActiveWorkbook.SaveAs "C:\" & FileName
    11.   ElseIf TimeNow >= CDate(#10:00:01 PM#) And TimeNow <= CDate(#5:59:59 AM#) Then
    12.     ActiveWorkbook.SaveAs "C:\" & FileName
    13.   End If

    Your folders will also have to exist otherwise it will error out.. the create the folders you will need to create them one at a time, this example could go between the if and first elseif statemtent, and then replicate for the others..

    VB Code:
    1. On Error Resume Next 'will prevent the error if the path already exists
    2.   MkDir "C:\MyFilesOutput\"
    3.   MkDir "C:\MyFilesOutput\Mornings\"
    4.   On Error GoTo 0 'switch off error trapping
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    54

    Resolved Re: Save as button for Excel

    Thanks Danny your a star, it works fine now.


    John

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