Results 1 to 4 of 4

New to VBA - VBA Before Save & Before Email

Threaded View

giggles2005 New to VBA - VBA Before Save... 05-10-2020, 06:49 AM
LJMetzger Re: New to VBA - VBA Before... 07-02-2020, 02:49 PM
giggles2005 Re: New to VBA - VBA Before... 07-28-2020, 05:52 AM
LJMetzger Re: New to VBA - VBA Before... 07-28-2020, 09:00 AM
  1. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: New to VBA - VBA Before Save & Before Email

    Hi giggles2005 and welcome to ExcelForum,

    Notes:
    a. Link ComboBoxes to Underyling cells
    b. Description of Product MUST be Mandatory - otherwise how do you know what is being purchased.
    c. Removed TextBoxes in Column E (VAT).
    d. Option Explicit
    e. Added check for all BLANK Data Lines.

    It is highly recommended that you avoid 'ActiveX' Controls (ComboBoxes, TextBoxes, CommandButtons) and use 'Forms' Controls instead. 'ActiveX' does not play well with others, and can cause Controls to move or worse.

    Try the following code which is included in the modified copy of your sample workbook (attached):

    In the Code Module For Sheet 'PO Request Form':
    Option Explicit
    
    Private Sub CommandButton1_Click()
    
      Dim OutlookApp As Object
      Dim OutlookMail As Object
      
      Dim sErrorMessage As String
      
      'Verify that required Inputs are NOT BLANK
      'Display an Error Message and Exit if they are BLANK
      sErrorMessage = VerifyInputs()
      If Len(sErrorMessage) > 0 Then
        MsgBox sErrorMessage
        Exit Sub
      End If
      
      Set OutlookApp = CreateObject("Outlook.Application")
      Set OutlookMail = OutlookApp.CreateItem(0)
      
      With OutlookMail
        .To = "giggles2005-2006@hotmail.co.uk"
        .Subject = "PO Request"
        .Body = " Hi Team, please can you raise the attached?"
        .attachments.Add ActiveWorkbook.FullName
        .Display
    End With
    
      Set OutlookMail = Nothing
      Set OutlookApp = Nothing
    
    End Sub
    In Ordinary Code Module ModVerifyInputs:
    Option Explicit
    
    Function VerifyInputs() As String
      'This returns an Error Message String if any of the required inputs are BLANK.
      '
      'The Null String is returned if there are no MISSING ITEMS
    
      Dim wb As Workbook
      Dim ws As Worksheet
    
      Dim i As Long
      Dim iArrayOfDataRowNumbers(1 To 5)
      Dim iColumnIndex As Long
      Dim iCountOfNonBlankMandatoryItemsThisRow As Long
      Dim iNumberOfMandatoryItemsPerRow As Long
      Dim iRow As Long
      Dim iRowIndex As Long
      
      Dim bComboBoxesRequired As Boolean
      Dim bHaveNonBlankDataEntry As Boolean
      
      Dim sArrayOfAdminCells(1 To 6) As String
      Dim sArrayOfMandatoryDataColumns(1 To 3) As String
      Dim sArrayOfSemiMandatoryDataColumns(1 To 2) As String
      Dim sBadListAdmin As String
      Dim sBadListData As String
      Dim sCellAddress As String
      Dim sColumn As String
      Dim sErrorMessage As String
      Dim sValue As String
      
      'Pseudo Constant
      sArrayOfAdminCells(1) = "C18"
      sArrayOfAdminCells(2) = "C21"
      sArrayOfAdminCells(3) = "C24"
      sArrayOfAdminCells(4) = "C31"
      sArrayOfAdminCells(5) = "C35"
      sArrayOfAdminCells(6) = "C39"
      
      sArrayOfMandatoryDataColumns(1) = "C"
      sArrayOfMandatoryDataColumns(2) = "D"
      sArrayOfMandatoryDataColumns(3) = "E"
      sArrayOfSemiMandatoryDataColumns(1) = "G"
      sArrayOfSemiMandatoryDataColumns(2) = "H"
      
      iArrayOfDataRowNumbers(1) = 45
      iArrayOfDataRowNumbers(2) = 47
      iArrayOfDataRowNumbers(3) = 49
      iArrayOfDataRowNumbers(4) = 51
      iArrayOfDataRowNumbers(5) = 53
      
      
      'Create Worksheet Object
      Set wb = ThisWorkbook    'The file that contains this code
      Set ws = wb.Sheets("PO Request Form")
      
      
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      'Verify Admin Data
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      For i = LBound(sArrayOfAdminCells) To UBound(sArrayOfAdminCells)
      
        'Get the Next Cell Address
        'Get the Next Value (remove leading/trailing Spaces)
        sCellAddress = sArrayOfAdminCells(i)
        sValue = Trim(ws.Range(sCellAddress).Value)
        'Debug.Print i, sCellAddress, Len(sValue)  'Output to Immediate Window (Ctrl G) in the debugger
        
        'Add to the 'Bad List' if the Value is BLANK
        If Len(sValue) = 0 Then
          If Len(sBadListAdmin) = 0 Then
            sBadListAdmin = "The following Administrative cells are NOT allowed to be BLANK:" & vbCrLf & "   " & sCellAddress
          Else
            sBadListAdmin = sBadListAdmin & "  " & sCellAddress
          End If
        End If
        
      Next i
      
      
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      'Verify Data
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      
      'Get the Number of 'Mandatory Items' Per Row
      iNumberOfMandatoryItemsPerRow = UBound(sArrayOfMandatoryDataColumns) - LBound(sArrayOfMandatoryDataColumns) + 1
      
      For iRowIndex = LBound(iArrayOfDataRowNumbers) To UBound(iArrayOfDataRowNumbers)
      
        'Get the Row Number
        iRow = iArrayOfDataRowNumbers(iRowIndex)
      
        'Initialize Variables
        bComboBoxesRequired = False
        iCountOfNonBlankMandatoryItemsThisRow = 0
        
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'Get the Count of NonBlank Mandatory Items on this Row
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        For iColumnIndex = LBound(sArrayOfMandatoryDataColumns) To UBound(sArrayOfMandatoryDataColumns)
      
          'Get the Next Cell Address by concatenating the Column Letter and Row Number
          'Get the Next Value (remove leading/trailing Spaces)
          sColumn = sArrayOfMandatoryDataColumns(iColumnIndex)
          sCellAddress = sColumn & iRow
          sValue = Trim(ws.Range(sCellAddress).Value)
        
          'Increment the Count of Non-Blank Mandatory Items on this Row if the Value is NOT BLANK
          If Len(sValue) > 0 Then
            bHaveNonBlankDataEntry = True
            iCountOfNonBlankMandatoryItemsThisRow = iCountOfNonBlankMandatoryItemsThisRow + 1
          End If
        
        Next iColumnIndex
      
      
      
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'Process this Column only if there is at least one NOT BLANK 'Mandatory Item'
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        If iCountOfNonBlankMandatoryItemsThisRow > 0 Then
        
          For iColumnIndex = LBound(sArrayOfMandatoryDataColumns) To UBound(sArrayOfMandatoryDataColumns)
      
            'Get the Next Cell Address by concatenating the Column Letter and Row Number
            'Get the Next Value (remove leading/trailing Spaces)
            sColumn = sArrayOfMandatoryDataColumns(iColumnIndex)
            sCellAddress = sColumn & iRow
            sValue = Trim(ws.Range(sCellAddress).Value)
        
            'Add to the 'Bad List' if the Value is BLANK
            If Len(sValue) = 0 Then
              If Len(sBadListData) = 0 Then
                sBadListData = "The following Data cells are NOT allowed to be BLANK:" & vbCrLf & "   " & sCellAddress
              Else
                sBadListData = sBadListData & "  " & sCellAddress
              End If
            End If
        
          Next iColumnIndex
        
        End If
        
        
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'Process the ComboBox Rows only if all the 'Mandatory Items' on this Row Have Data
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        If iCountOfNonBlankMandatoryItemsThisRow = iNumberOfMandatoryItemsPerRow Then
        
          For iColumnIndex = LBound(sArrayOfSemiMandatoryDataColumns) To UBound(sArrayOfSemiMandatoryDataColumns)
      
            'Get the Next Cell Address by concatenating the Column Letter and Row Number
            'Get the Next Value (remove leading/trailing Spaces)
            sColumn = sArrayOfSemiMandatoryDataColumns(iColumnIndex)
            sCellAddress = sColumn & iRow
            sValue = Trim(ws.Range(sCellAddress).Value)
        
            'Add to the 'Bad List' if the Value is BLANK
            If Len(sValue) = 0 Then
              If Len(sBadListData) = 0 Then
                sBadListData = "The following Data cells are NOT allowed to be BLANK:" & vbCrLf & "   " & sCellAddress
              Else
                sBadListData = sBadListData & "  " & sCellAddress
              End If
            End If
        
          Next iColumnIndex
        
        End If
            
      Next iRowIndex
      
      
      
      'Create Error Text if there are Bad Inputs or No Inputs
      If Len(sBadListAdmin) > 0 Or Len(sBadListData) > 0 Then
        sErrorMessage = "Email Request is NOT ALLOWED to be sent."
      End If
      
      If Len(sBadListAdmin) > 0 Then
        sErrorMessage = sErrorMessage & vbCrLf & vbCrLf & _
                        sBadListAdmin
      End If
      
      If Len(sBadListData) > 0 Then
        sErrorMessage = sErrorMessage & vbCrLf & vbCrLf & _
                        sBadListData
      End If
      
      If Len(sErrorMessage) = 0 And bHaveNonBlankDataEntry = False Then
        sErrorMessage = "Email Request is NOT ALLOWED to be sent." & vbCrLf & _
                        "All Data Entry Cells are BLANK."
      End If
      
      'Set the Return Value
      VerifyInputs = sErrorMessage
    
     'Clear Object Pointers
     Set wb = Nothing
     Set ws = Nothing
    
    End Function
    To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. Dim i as Integer). http://www.cpearson.com/excel/DeclaringVariables.aspx

    Lewis

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 5
    Last Post: 04-29-2020, 11:12 AM
  2. [SOLVED] Rule to file email in email folder, save the attachment to desk top and update spreadsheet
    By JET2011 in forum Outlook Formatting & Functions
    Replies: 49
    Last Post: 08-29-2018, 12:49 PM
  3. [VBA] Save Range as picture, save to file, attach to email
    By Armitage2k in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-23-2018, 07:34 AM
  4. Save Email into specific folder with file name from body of email
    By hudson andrew in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-21-2016, 02:27 PM
  5. Macro - Save Word as PDF with Unique Name and Email PDF to specified email address.
    By newbie1234 in forum Word Programming / VBA / Macros
    Replies: 6
    Last Post: 07-08-2014, 11:54 PM
  6. Replies: 0
    Last Post: 11-22-2012, 08:42 AM
  7. edit, save as new and email to multiple email addresses
    By murphyx232 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-20-2007, 02:37 PM

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