+ Reply to Thread
Results 1 to 12 of 12

Need to Save current sheet to one of two files depending on data ( Q or I)

Hybrid View

  1. #1
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Need to Save current sheet to one of two files depending on data ( Q or I)

    Hi there,

    I have some code below that converts the current sheet to a PDF then uses data from B16, H8 and H10 to save as its filename, then it saves it to a certain file.

    What I would like it to do is :

    1) Convert active sheet to PDF.
    2) Use the data from cells B16,H8 and H10 to save as the filename.
    3) Check to see if cell H8 contains the letter Q if true then save file to C:\Documents and Settings\carl.walker\My Documents\Quotations\.
    4) Check to see if cell H8 contains the letter I if true then save file to C:\Documents and Settings\carl.walker\My Documents\Invoices\.
    5) Oh and still use the IF's and msgboxes.

    I'm not really sure where to start to honest so any help would be much appreciated

    Thanks in advance
    Carl

    Sub SaveActivesheet()
    
     Const strPath As String = "C:\Documents and Settings\carl.walker\My Documents\Reports\"
     Dim strFile As String
    
     With Sheet2
     'check that Account Details are present (customer's name)'
     If .Range("B16").Value = "" Then
     MsgBox "Please enter Account Details by using the ADD ACCOUNT Userform provided. ", _
     vbExclamation, "Missing Account"
     
     'check that Date is present'
     ElseIf .Range("H10").Value = "" Then
     MsgBox "Please enter a date. ", _
     vbExclamation, "Missing Date"
    
     'check that a Quotation or Invoice number is present '
     ElseIf .Range("H8").Value = "" Then
     MsgBox "Please enter a Quotation or Invoice number. ", _
     vbExclamation, "Missing Quotation or Invoice number"
    
    Else
    strFile = .Range("B16").Value & " " & .Range("H10").Value & " " & .Range("H8").Value
     .Copy 'Copy sheet2 to a new workbook
    
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
     strPath & strFile & ".pdf", Quality:= _
     xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
     OpenAfterPublish:=False
    
     MsgBox "This has now been converted to PDF and has been saved in the Quotations and Invoices folder. ", _
     vbExclamation, "Successfully Saved"
     
     End If
     End With
     End Sub
    Last edited by carlandtina02; 06-20-2013 at 12:49 PM. Reason: grammer lol

  2. #2
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Need to Save current sheet to one of two files depending on data ( Q or I)

    Try this..

            Sub SaveActivesheet()
    
             Const strPath As String = "C:\Documents and Settings\carl.walker\My Documents\"
             Dim strFile As String
    
             With Sheet2
             'check that Account Details are present (customer's name)'
             If .Range("B16").Value = "" Then
             MsgBox "Please enter Account Details by using the ADD ACCOUNT Userform provided. ", _
             vbExclamation, "Missing Account"
             
             'check that Date is present'
             ElseIf .Range("H10").Value = "" Then
             MsgBox "Please enter a date. ", _
             vbExclamation, "Missing Date"
    
             'check that a Quotation or Invoice number is present '
             ElseIf .Range("H8").Value = "" Then
             MsgBox "Please enter a Quotation or Invoice number. ", _
             vbExclamation, "Missing Quotation or Invoice number"
    
            Else
        Select Case .Range("H8")
            Case "Q":
                strPath = strPath & "Quotations\"
            Case "I":
                strPath = strPath & "Invoice\"
            Case Else
                strPath = strPath & "Reports\"
        End Select
    
            strFile = .Range("B16").Value & " " & .Range("H10").Value & " " & .Range("H8").Value
             .Copy 'Copy sheet2 to a new workbook
    
             ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
             strPath & strFile & ".pdf", Quality:= _
             xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
             OpenAfterPublish:=False
    
             MsgBox "This has now been converted to PDF and has been saved in the Quotations and Invoices folder. ", _
             vbExclamation, "Successfully Saved"
             
             End If
             End With
             End Sub
    Regards!
    =DEC2HEX(3563)

    If you like someone's answer, click the star to give them a reputation point for that answer...

  3. #3
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Need to Save current sheet to one of two files depending on data ( Q or I)

    Hi Deb,

    Thanks for replying, I've just ran the code but it's sprung up with a compile error (assignment to constant not permitted)?

            Else
        Select Case .Range("H8")
            Case "Q":
                strPath = strPath & "Quotations\"
            Case "I":
                strPath = strPath & "Invoice\"
            Case Else
                strPath = strPath & "Reports\"
        End Select
    Also forgot to add that I have taken away the reports folder and replaced it with a Quotation folder and a Invoice folder.


    Regards
    Carl
    Last edited by carlandtina02; 06-20-2013 at 06:11 PM. Reason: grammer lol

  4. #4
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Need to Save current sheet to one of two files depending on data ( Q or I)

    OOPS.. my appologize..

    Please delete keyword Const from the 1st line, and use just Dim

  5. #5
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Need to Save current sheet to one of two files depending on data ( Q or I)

    Hi Deb,

    I just tried and its flagged up as "syntax error"?

      Sub SaveActivesheet()
    
            Dim strPath As String = "C:\Documents and Settings\carl.walker\My Documents\"         
             Dim strFile As String
    
             With Sheet2
             'check that Account Details are present (customer's name)'
             If .Range("B16").Value = "" Then
             MsgBox "Please enter Account Details by using the ADD ACCOUNT Userform provided. ", _
             vbExclamation, "Missing Account"
             
             'check that Date is present'
             ElseIf .Range("H10").Value = "" Then
             MsgBox "Please enter a date. ", _
             vbExclamation, "Missing Date"
    
             'check that a Quotation or Invoice number is present '
             ElseIf .Range("H8").Value = "" Then
             MsgBox "Please enter a Quotation or Invoice number. ", _
             vbExclamation, "Missing Quotation or Invoice number"
    
            Else
        Select Case .Range("H8")
            Case "Q":
                strPath = strPath & "Quotations\"
            Case "I":
                strPath = strPath & "Invoice\"
            Case Else
                strPath = strPath & "Reports\"
        End Select
    
            strFile = .Range("B16").Value & " " & .Range("H10").Value & " " & .Range("H8").Value
             .Copy 'Copy sheet2 to a new workbook
    
             ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
             strPath & strFile & ".pdf", Quality:= _
             xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
             OpenAfterPublish:=False
    
             MsgBox "This has now been converted to PDF and has been saved in the Quotations and Invoices folder. ", _
             vbExclamation, "Successfully Saved"
             
             
             End If
             End With
             End Sub
    Kind Regards
    Carl

  6. #6
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Need to Save current sheet to one of two files depending on data ( Q or I)

    Hi,

    Can anyone help with this pretty pleeeeaaaaaaase?

    Regards
    Carl

  7. #7
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Need to Save current sheet to one of two files depending on data ( Q or I)

    Hi Folks,

    Can anyone give me any advice with the above please?


    Thanks in advance
    Kind regards
    Carl

  8. #8
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Need to Save current sheet to one of two files depending on data ( Q or I)

    Hi Carl!

    Sorry.. I missed this post.. ;(

    try this..
      Sub SaveActivesheet()
    
            Dim strPath As String
            strPath = "C:\Documents and Settings\carl.walker\My Documents\"
             Dim strFile As String
    
             With Sheet2
             'check that Account Details are present (customer's name)'
             If .Range("B16").Value = "" Then
             MsgBox "Please enter Account Details by using the ADD ACCOUNT Userform provided. ", _
             vbExclamation, "Missing Account"
             
             'check that Date is present'
             ElseIf .Range("H10").Value = "" Then
             MsgBox "Please enter a date. ", _
             vbExclamation, "Missing Date"
    
             'check that a Quotation or Invoice number is present '
             ElseIf .Range("H8").Value = "" Then
             MsgBox "Please enter a Quotation or Invoice number. ", _
             vbExclamation, "Missing Quotation or Invoice number"
    
            Else
        Select Case .Range("H8")
            Case "Q":
                strPath = strPath & "Quotations\"
            Case "I":
                strPath = strPath & "Invoice\"
            Case Else
                strPath = strPath & "Reports\"
        End Select
    
            strFile = .Range("B16").Value & " " & .Range("H10").Value & " " & .Range("H8").Value
             .Copy 'Copy sheet2 to a new workbook
    
             ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
             strPath & strFile & ".pdf", Quality:= _
             xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
             OpenAfterPublish:=False
    
             MsgBox "This has now been converted to PDF and has been saved in the Quotations and Invoices folder. ", _
             vbExclamation, "Successfully Saved"
             
             
             End If
             End With
             End Sub

  9. #9
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Need to Save current sheet to one of two files depending on data ( Q or I)

    Cheers Deb,

    Not a problem, I've just ran the code and it worked fine, Just one question..is there a way to make case Q or I non case sensitive because it saves to the report folder when they are in lowercase?

    Thanks for your help with this, it's much apreciated!

    Regards
    Carl

  10. #10
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Need to Save current sheet to one of two files depending on data ( Q or I)

    Try This..

    Sub SaveActivesheet()
    
    Dim strPath As String
    strPath = "C:\Documents and Settings\carl.walker\My Documents\"
    Dim strFile As String
    
    With Sheet2
        'check that Account Details are present (customer's name)'
        If .Range("B16").Value = "" Then
            MsgBox "Please enter Account Details by using the ADD ACCOUNT Userform provided. ", _
            vbExclamation, "Missing Account"
            
            'check that Date is present'
        ElseIf .Range("H10").Value = "" Then
            MsgBox "Please enter a date. ", _
            vbExclamation, "Missing Date"
            
            'check that a Quotation or Invoice number is present '
        ElseIf .Range("H8").Value = "" Then
            MsgBox "Please enter a Quotation or Invoice number. ", _
            vbExclamation, "Missing Quotation or Invoice number"
            
        Else
            Select Case .Range("H8")
            Case "Q", "q"
                strPath = strPath & "Quotations\"
            Case "I", "i"
                strPath = strPath & "Invoice\"
            Case Else
                strPath = strPath & "Reports\"
            End Select
            
            strFile = .Range("B16").Value & " " & .Range("H10").Value & " " & .Range("H8").Value
            .Copy 'Copy sheet2 to a new workbook
            
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            strPath & strFile & ".pdf", Quality:= _
            xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
            OpenAfterPublish:=False
            
            MsgBox "This has now been converted to PDF and has been saved in the Quotations and Invoices folder. ", _
            vbExclamation, "Successfully Saved"
            
            
        End If
    End With
    End Sub

  11. #11
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Need to Save current sheet to one of two files depending on data ( Q or I)

    Thanks Deb,

    It worked a treat.


    Kind Regards
    Carl

  12. #12
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Need to Save current sheet to one of two files depending on data ( Q or I)

    Thanks for the feedback..

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1