+ Reply to Thread
Results 1 to 14 of 14

VBA custom format adjustment

Hybrid View

  1. #1
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    VBA custom format adjustment

    Hi,

    The current attached VBA generates an e-mail per each Name from sheet "List" and attaches an xlsx in each e-mail with the associated data for that particular Name.

    Can someone help by adjusting the code so the xlsx file attached in each e-mail will look exactly like "desired result.xlsx" attached in this post?

    (the other functionalities of the VBA should remain the same)
    Attached Files Attached Files
    Last edited by tyxanu; 05-10-2023 at 06:33 AM.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: VBA custom format adjustment

    Hi there,

    See if the attached version of your workbook does what you need - it uses the following code:

    
    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Const miFIRST_DATA_ROW_NO   As Integer = 3
    Const msLIST_SHEET_NAME     As String = "List"
    Const msNAME_COLUMN         As String = "B"
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub ScanThroughNames()
    
        Dim sCurrentName    As String
        Dim rNameColumn     As Range
        Dim iLastRowNo      As Integer
        Dim objOutlook      As Object
        Dim wksList         As Worksheet
        Dim iRowNo          As Integer
    
        Call CreateOutlookInstance(objOutlook:=objOutlook)
    
        Set wksList = ThisWorkbook.Worksheets(msLIST_SHEET_NAME)
    
        Set rNameColumn = wksList.Columns(msNAME_COLUMN)
    
        With wksList.UsedRange
            iLastRowNo = .Rows(.Rows.Count).Row
        End With
    
        sCurrentName = vbNullString
    
        For iRowNo = miFIRST_DATA_ROW_NO To iLastRowNo
    
            If rNameColumn.Cells(iRowNo, 1).Value <> sCurrentName Then
    
                sCurrentName = rNameColumn.Cells(iRowNo, 1).Value
    
                Call CreateWorkbook(wksList, sCurrentName, iLastRowNo)
    
            End If
    
        Next iRowNo
    
        Set objOutlook = Nothing
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub CreateOutlookInstance(objOutlook As Object)
    
        On Error Resume Next
            Set objOutlook = Nothing
            Set objOutlook = GetObject(, "Outlook.Application")
        On Error GoTo 0
    
        If objOutlook Is Nothing Then
            Set objOutlook = CreateObject("Outlook.Application")
        End If
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub CreateWorkbook(wksList As Worksheet, sCurrentName As String, _
                               iLastRowNo As Integer)
    
        Dim wksCopyOfList   As Worksheet
        Dim rNameColumn     As Range
        Dim rNameCell       As Range
        Dim iRowNo          As Integer
    
        wksList.Copy
        Set wksCopyOfList = ActiveSheet
    
        Set rNameColumn = wksCopyOfList.Columns(msNAME_COLUMN)
    
        For iRowNo = iLastRowNo To miFIRST_DATA_ROW_NO Step -1
    
            Set rNameCell = rNameColumn.Cells(iRowNo, 1)
    
            If rNameCell.Value <> sCurrentName Then
                rNameCell.EntireRow.Delete
            End If
    
        Next iRowNo
    
        Call SaveWorkbook(sCurrentName:=sCurrentName)
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub SaveWorkbook(sCurrentName As String)
    
        Const sADDRESS_COLUMN   As String = "R"
        Const sEXTENSION        As String = ".xlsx"
    
        Dim sEmailAddress       As String
        Dim sFullName           As String
        Dim sFilePath           As String
        Dim sFileName           As String
    
        sFilePath = Environ$("TEMP")
        sFileName = sCurrentName
    
        sFullName = sFilePath & "\" & sFileName & sEXTENSION
    
        sEmailAddress = ActiveSheet.Range(sADDRESS_COLUMN & miFIRST_DATA_ROW_NO).Value
    
        ActiveWorkbook.SaveAs Filename:=sFullName, FileFormat:=xlOpenXMLWorkbook
        ActiveWorkbook.Close
    
        Call CreateEmail(sCurrentName:=sCurrentName, sEmailAddress:=sEmailAddress, _
                         sFullName:=sFullName)
    
        Kill PathName:=sFullName
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub CreateEmail(sCurrentName As String, sEmailAddress As String, _
                            sFullName As String)
    
        Dim sSignature  As String
        Dim objOutlook  As Object
        Dim objEmail    As Object
    
        Set objOutlook = GetObject(, "Outlook.Application")
        Set objEmail = objOutlook.CreateItem(0)
    
        With objEmail
    
            .Display
    
            sSignature = .HTMLBody
    
            .To = sEmailAddress
            .Subject = "Requests"
    
            .HTMLBody = "Dear " & sCurrentName & "," & _
                        "<br><br>" & _
                        "Please find attached a list of YOUR OPEN REQUESTS. " & _
                        "Please review the open requests and update ONLY the last 3 blue columns with the current status." & _
                        "<br><br>" & _
                        "Thank you and Best Regards," & _
                         sSignature
    
            .Attachments.Add sFullName
            .Display
            '.Send - Replace with this to have it sent directly'
        
        End With
    
    End Sub
    The highlighted values may be changed to suit your requirements.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: VBA custom format adjustment

    Hi again,

    The attached version should ensure that colour schemes are copied correctly from the source workbook.

    The highlighted line has been added to the existing routine:

    
    Private Sub CreateWorkbook(wksList As Worksheet, sCurrentName As String, _
                               iLastRowNo As Integer)
    
        Dim wksCopyOfList   As Worksheet
        Dim rNameColumn     As Range
        Dim rNameCell       As Range
        Dim iRowNo          As Integer
    
        wksList.Copy
        Set wksCopyOfList = ActiveSheet
    
        Set rNameColumn = wksCopyOfList.Columns(msNAME_COLUMN)
    
        For iRowNo = iLastRowNo To miFIRST_DATA_ROW_NO Step -1
    
            Set rNameCell = rNameColumn.Cells(iRowNo, 1)
    
            If rNameCell.Value <> sCurrentName Then
                rNameCell.EntireRow.Delete
            End If
    
        Next iRowNo
    
        ActiveWorkbook.ApplyTheme ThisWorkbook.FullName
    
        Call SaveWorkbook(sCurrentName:=sCurrentName)
    
    End Sub

    Hope this helps.

    Regards,

    Greg M
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA custom format adjustment

    Hello Greg,

    This seems fine. I would have one more requirement if you would be able to assist:

    I would like to remove Column R (e-mail address) and have the code perform a vlookup directly by looking for the Names (from Column B) in Sheet "Addresses" and generate the e-mail accordingly (with the relevant data) to the Name's contact stated there.

    Therefor, the code should attach the xlsx containing Sheet "List" in whatever form and format I adjust it (even if I add extra columns/remove columns/change format/etc).

    Huge thanks for this

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: VBA custom format adjustment

    Ok, I'll see what I can do tomorrow

  6. #6
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: VBA custom format adjustment

    Hi again,

    Many thanks for your feedback and also for the Reputation increase - much appreciated!

    The attached workbook is a slightly modified version of the one I posted previously.

    I've added two named cells on the "Addresses" worksheet - the columns which contain these cells may be hidden if required. I've also deleted column "R" from the "List" worksheet.

    The code involved is as follows:

    
    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Const miFIRST_DATA_ROW_NO   As Integer = 3
    Const msLIST_SHEET_NAME     As String = "List"
    Const msNAME_COLUMN         As String = "B"
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private mobjOutlook         As Object
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub ScanThroughNames()
    
        Dim sCurrentName    As String
        Dim rNameColumn     As Range
        Dim iLastRowNo      As Integer
        Dim objOutlook      As Object
        Dim wksList         As Worksheet
        Dim iRowNo          As Integer
    
        Call CreateOutlookInstance(objOutlook:=objOutlook)
    
        Set wksList = ThisWorkbook.Worksheets(msLIST_SHEET_NAME)
    
        Set rNameColumn = wksList.Columns(msNAME_COLUMN)
    
        With wksList.UsedRange
            iLastRowNo = .Rows(.Rows.Count).Row
        End With
    
        sCurrentName = vbNullString
    
        For iRowNo = miFIRST_DATA_ROW_NO To iLastRowNo
    
            If rNameColumn.Cells(iRowNo, 1).Value <> sCurrentName Then
    
                sCurrentName = rNameColumn.Cells(iRowNo, 1).Value
    
                Call CreateWorkbook(wksList, sCurrentName, iLastRowNo)
    
            End If
    
        Next iRowNo
    
        Set objOutlook = Nothing
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub CreateOutlookInstance(objOutlook As Object)
    
        On Error Resume Next
            Set mobjOutlook = Nothing
            Set mobjOutlook = GetObject(, "Outlook.Application")
        On Error GoTo 0
    
        If objOutlook Is Nothing Then
            Set mobjOutlook = CreateObject("Outlook.Application")
        End If
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub CreateWorkbook(wksList As Worksheet, sCurrentName As String, _
                               iLastRowNo As Integer)
    
        Dim wksCopyOfList   As Worksheet
        Dim rNameColumn     As Range
        Dim rNameCell       As Range
        Dim iRowNo          As Integer
    
        wksList.Copy
        Set wksCopyOfList = ActiveSheet
    
        Set rNameColumn = wksCopyOfList.Columns(msNAME_COLUMN)
    
        For iRowNo = iLastRowNo To miFIRST_DATA_ROW_NO Step -1
    
            Set rNameCell = rNameColumn.Cells(iRowNo, 1)
    
            If rNameCell.Value <> sCurrentName Then
                rNameCell.EntireRow.Delete
            End If
    
        Next iRowNo
    
        ActiveWorkbook.ApplyTheme ThisWorkbook.FullName
    
        Call SaveWorkbook(sCurrentName:=sCurrentName)
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub SaveWorkbook(sCurrentName As String)
    
        Const sADDRESSES_SHEET_NAME As String = "Addresses"
        Const sEXTENSION            As String = ".xlsx"
    
        Dim sEmailAddress           As String
        Dim wksAddresses            As Worksheet
        Dim sFullName               As String
        Dim sFilePath               As String
        Dim sFileName               As String
    
        sFilePath = Environ$("TEMP")
        sFileName = sCurrentName
    
        sFullName = sFilePath & "\" & sFileName & sEXTENSION
    
        Set wksAddresses = ThisWorkbook.Worksheets(sADDRESSES_SHEET_NAME)
    
        wksAddresses.Range("ptrCurrentName").Value = sCurrentName
    
        sEmailAddress = wksAddresses.Range("ptrCurrentAddress").Value
    
        ActiveWorkbook.SaveAs Filename:=sFullName, FileFormat:=xlOpenXMLWorkbook
        ActiveWorkbook.Close
    
        Call CreateEmail(sCurrentName:=sCurrentName, sEmailAddress:=sEmailAddress, _
                         sFullName:=sFullName)
    
        Kill PathName:=sFullName
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub CreateEmail(sCurrentName As String, sEmailAddress As String, _
                            sFullName As String)
    
        Const iMAIL_ITEM    As Integer = 0
    
        Dim sSignature      As String
        Dim objEmail        As Object
    
        Set objEmail = mobjOutlook.CreateItem(iMAIL_ITEM)
    
        With objEmail
    
            .Display
    
            sSignature = .HTMLBody
    
            .To = sEmailAddress
            .Subject = "Requests"
    
            .HTMLBody = "Dear " & sCurrentName & "," & _
                        "<br><br>" & _
                        "Please find attached a list of YOUR OPEN REQUESTS. " & _
                        "Please review the open requests and update ONLY the last 3 blue columns with the current status." & _
                        "<br><br>" & _
                        "Thank you and Best Regards," & _
                         sSignature
    
            .Attachments.Add sFullName
    '       Include the following line to send the email automatically
    '''        .Send
        
        End With
    
    End Sub

    Hope this helps - as before, please let me know how you get on.

    Regards,

    Greg M
    Last edited by Greg M; 05-12-2023 at 07:40 AM. Reason: Code amended very slightly

  7. #7
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA custom format adjustment

    Hi Greg and thanks again ,

    I don't see the attachment in this most recent post. Was it intended to be here?

  8. #8
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: VBA custom format adjustment

    Oops - mea culpa!
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA custom format adjustment

    Hi Greg,

    Sorry, I was away for a couple of days.

    So I have adapted the content and attached my workbook with your code, to be as close as possible to my official data:

    a) column K contains a dropdown for the person to pick some options
    b) based on column K option, column M will get populated accordingly (formula based on hidden Sheet2)

    -----I have filled rows up until 9000 with the above data validation and formula because this is needed to cover as much rows as the user might input------

    Problem: Something is not working right as it seems Excel freezes when I hit "Generate emails". Can you also test based on the attached file?

    Does it have something to do with the 9000 empty cells filled with data validation and formula? (K and M)
    (However, the code should stop and generate/send emails only up until row 7 in our case, based on data from A to I)

    Also, I would need to understand whether the code vlooksup the name from column B, into column A or column D from "Addresses"?

    Appreciate your help!
    Attached Files Attached Files
    Last edited by tyxanu; 05-18-2023 at 06:55 AM.

  10. #10
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA custom format adjustment

    Hi Greg

    Have you got the chance to review the above reply?

  11. #11
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: VBA custom format adjustment

    Hi again,

    Hey, nobody said anything about nine thousand rows!

    That sort of number makes a BIG difference to the approach used in producing what is required!

    The original approach deleted unwanted rows on a row-by-row basis - it takes quite some time to individually delete the large number of rows involved!


    The attached version prefixes each instance of the current Addressee Name with an "AAAAA" prefix and then sorts the worksheet to bring the current Addressee to the top of the list. The prefix is then removed.

    When this has been done, the unwanted Addressee rows can be deleted in bulk - this takes far less time than deleting them individually.

    The following code is used:

    
    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Const miFIRST_DATA_ROW_NO   As Integer = 3
    Const msLIST_SHEET_NAME     As String = "List"
    Const msNAME_COLUMN         As String = "B"
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private mobjOutlook         As Object
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub ScanThroughNames()
    
        Dim sCurrentName    As String
        Dim rNameColumn     As Range
        Dim iLastRowNo      As Integer
        Dim objOutlook      As Object
        Dim wksList         As Worksheet
        Dim iRowNo          As Integer
    
        Call CreateOutlookInstance(objOutlook:=objOutlook)
    
        Set wksList = ThisWorkbook.Worksheets(msLIST_SHEET_NAME)
    
        Set rNameColumn = wksList.Columns(msNAME_COLUMN)
    
        With wksList.UsedRange
            iLastRowNo = .Rows(.Rows.Count).Row
        End With
    
        sCurrentName = vbNullString
    
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
    
            For iRowNo = miFIRST_DATA_ROW_NO To iLastRowNo
    
                If rNameColumn.Cells(iRowNo, 1).Value <> sCurrentName Then
    
                    sCurrentName = rNameColumn.Cells(iRowNo, 1).Value
    
                If sCurrentName <> vbNullString Then
                    Call CreateWorkbook(wksList, sCurrentName, iLastRowNo)
                End If
    
            End If
    
            Next iRowNo
    
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    
        Set objOutlook = Nothing
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub CreateOutlookInstance(objOutlook As Object)
    
        On Error Resume Next
            Set mobjOutlook = Nothing
            Set mobjOutlook = GetObject(, "Outlook.Application")
        On Error GoTo 0
    
        If objOutlook Is Nothing Then
            Set mobjOutlook = CreateObject("Outlook.Application")
        End If
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub CreateWorkbook(wksList As Worksheet, sCurrentName As String, _
                               iLastRowNo As Integer)
    
        Dim wksCopyOfList As Worksheet
    
        wksList.Copy
    
        Set wksCopyOfList = ActiveSheet
    
        Call MoveCurrentNameRowsToTopOfList(wksCopyOfList:=wksCopyOfList, _
                                            sCurrentName:=sCurrentName, _
                                            iLastRowNo:=iLastRowNo)
    
        Call DeleteUnwantedRows(wksCopyOfList:=wksCopyOfList, _
                                iLastRowNo:=iLastRowNo)
    
        ActiveWorkbook.ApplyTheme ThisWorkbook.FullName
    
        Call SaveWorkbook(sCurrentName:=sCurrentName)
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub MoveCurrentNameRowsToTopOfList(wksCopyOfList As Worksheet, _
                                               sCurrentName As String, _
                                               iLastRowNo As Integer)
    
        Const sPREFIX_FOR_SORTING   As String = "AAAAA"
    
        Dim rRangeToSort            As Range
        Dim rNameColumn             As Range
        Dim rNameCells              As Range
    
        Set rNameColumn = wksCopyOfList.Columns(msNAME_COLUMN)
    
        With rNameColumn
    
            Set rNameCells = Range(.Rows(miFIRST_DATA_ROW_NO), _
                                   .Rows(iLastRowNo))
    
        End With
    
        rNameCells.Replace What:=sCurrentName, _
                           Replacement:=sPREFIX_FOR_SORTING & sCurrentName, _
                           LookAt:=xlWhole
    
            With wksCopyOfList
    
                Set rRangeToSort = Range(.Rows(miFIRST_DATA_ROW_NO), _
                                         .Rows(iLastRowNo))
    
            End With
    
            rRangeToSort.Sort Key1:=rNameCells.Cells(1, 1), Order1:=xlAscending, Header:=xlNo
    
        rNameCells.Replace What:=sPREFIX_FOR_SORTING, _
                           Replacement:=vbNullString, _
                           LookAt:=xlPart
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub DeleteUnwantedRows(wksCopyOfList As Worksheet, _
                                   iLastRowNo As Integer)
    
        Dim iFirstRowToDelete       As Integer
        Dim rRangeToDelete          As Range
        Dim rNameColumn             As Range
        Dim rNameCells              As Range
        Dim rNameCell               As Range
    
        Set rNameColumn = wksCopyOfList.Columns(msNAME_COLUMN)
    
        With rNameColumn
    
            Set rNameCells = Range(.Rows(miFIRST_DATA_ROW_NO), _
                                   .Rows(iLastRowNo))
    
        End With
    
        For Each rNameCell In rNameCells.Cells
    
            If rNameCell.Value <> rNameCell.Offset(1, 0).Value Then
                iFirstRowToDelete = rNameCell.Row + 1
                Exit For
            End If
    
        Next rNameCell
    
        With wksCopyOfList
    
            Set rRangeToDelete = Range(.Rows(iFirstRowToDelete), _
                                       .Rows(iLastRowNo))
    
        End With
    
        rRangeToDelete.EntireRow.Delete
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub SaveWorkbook(sCurrentName As String)
    
        Const sADDRESSES_SHEET_NAME As String = "Addresses"
        Const sEXTENSION            As String = ".xlsx"
    
        Dim sEmailAddress           As String
        Dim wksAddresses            As Worksheet
        Dim sFullName               As String
        Dim sFilePath               As String
        Dim sFileName               As String
    
        sFilePath = Environ$("TEMP")
        sFileName = sCurrentName
    
        sFullName = sFilePath & "\" & sFileName & sEXTENSION
    
        Set wksAddresses = ThisWorkbook.Worksheets(sADDRESSES_SHEET_NAME)
    
        wksAddresses.Range("ptrCurrentName").Value = sCurrentName
    
        sEmailAddress = wksAddresses.Range("ptrCurrentAddress").Value
    
        ActiveWorkbook.SaveAs Filename:=sFullName, FileFormat:=xlOpenXMLWorkbook
        ActiveWorkbook.Close
    
        Call CreateEmail(sCurrentName:=sCurrentName, sEmailAddress:=sEmailAddress, _
                         sFullName:=sFullName)
    
        Kill PathName:=sFullName
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub CreateEmail(sCurrentName As String, sEmailAddress As String, _
                            sFullName As String)
    
        Const iMAIL_ITEM    As Integer = 0
    
        Dim sSignature      As String
        Dim objEmail        As Object
    
        Set objEmail = mobjOutlook.CreateItem(iMAIL_ITEM)
    
        With objEmail
    
            .Display
    
            sSignature = .HTMLBody
    
            .To = sEmailAddress
            .Subject = "Requests"
    
            .HTMLBody = "Dear " & sCurrentName & "," & _
                        "<br><br>" & _
                        "Please find attached a list of YOUR OPEN REQUESTS. " & _
                        "Please review the open requests and update ONLY the last 3 blue columns with the current status." & _
                        "<br><br>" & _
                        "Thank you and Best Regards," & _
                         sSignature
    
            .Attachments.Add sFullName
    '       Include the following line to send the email automatically
    '''        .Send
        
        End With
    
    End Sub
    The highlighted values can be altered to suit your requirements.


    Hope this helps - as before, please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA custom format adjustment

    Hi Greg,

    1. Seems to work fine, except for the part with the addresses.

    If you run the code, for example:

    - to "[email protected]" it attaches John.xlsx and "Dear John..."
    - to "[email protected]" it attaches Richard.xlsx and "Dear Richard..."
    - to "[email protected]" it attaches Ben.xlsx and "Dear Ben..."

    The thing is that in Sheet "Addresses" columns A and B will contain around 2000 addresses, like a contact book. A static list, prone to review once in a while.

    The code should only lookup the Name (column B, Sheet "List") into Sheet "Addresses" column A and generate the e-mail text to the address (column B) matching that name.

    Is this possible? I think it's a much more clean and simple approach, what do you think?

    2. Second point I've wanted to ask you:

    - is the code able to generate/send more than let's say 50 e-mails at once? What about 80?

    Many thanks!
    Attached Files Attached Files
    Last edited by tyxanu; 05-24-2023 at 03:41 AM.

  13. #13
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: VBA custom format adjustment

    Hi again,

    Thanks for the feedback, and sorry, that was my fault!

    To try to speed things up I had disabled automatic calculation, but forgot that the email address of the current UserName must be determined (by calculation) each time. The highlighted additional line in the following routine will do what is required.

    
    Private Sub SaveWorkbook(sCurrentName As String)
    
        Const sADDRESSES_SHEET_NAME As String = "Addresses"
        Const sEXTENSION            As String = ".xlsx"
    
        Dim sEmailAddress           As String
        Dim wksAddresses            As Worksheet
        Dim sFullName               As String
        Dim sFilePath               As String
        Dim sFileName               As String
    
        sFilePath = Environ$("TEMP")
        sFileName = sCurrentName
    
        sFullName = sFilePath & "" & sFileName & sEXTENSION
    
        Set wksAddresses = ThisWorkbook.Worksheets(sADDRESSES_SHEET_NAME)
    
        wksAddresses.Range("ptrCurrentName").Value = sCurrentName
        wksAddresses.Calculate
    
        sEmailAddress = wksAddresses.Range("ptrCurrentAddress").Value
    
        ActiveWorkbook.SaveAs Filename:=sFullName, FileFormat:=xlOpenXMLWorkbook
        ActiveWorkbook.Close
    
        Call CreateEmail(sCurrentName:=sCurrentName, sEmailAddress:=sEmailAddress, _
                         sFullName:=sFullName)
    
        Kill PathName:=sFullName
    
    End Sub

    The code can create as many emails as you like, but a possible "bottleneck" is the number of emails which your email service provider will accept. In a different application (using Gmail via Outlook) I've restricted the size of a "batch" of emails to 80, and have not encountered problems to date.

    It should be a relatively straightforward job for you to send your emails in appropriately-sized batches.


    Regards,

    Greg M



    P.S.


    The thing is that in Sheet "Addresses" columns A and B will contain around 2000 addresses, like a contact book. A static list, prone to review once in a while.

    The code should only lookup the Name (column B, Sheet "List") into Sheet "Addresses" column A and generate the e-mail text to the address (column B) matching that name.

    Is this possible? I think it's a much more clean and simple approach, what do you think?

    This is the approach used in the current and previous workbooks.
    Attached Files Attached Files
    Last edited by Greg M; 05-24-2023 at 12:55 PM. Reason: P.S. added

  14. #14
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA custom format adjustment

    Greg, you're a god!

    Let me play around with it and test all the scenarios and I'll get back to you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How to change Pivot Chart axis number format to a custom date format
    By paulma1960 in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 01-11-2023, 03:16 PM
  2. Format Cells -> Custom Number with Custom Color
    By Cardan in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-22-2021, 08:19 AM
  3. Replies: 5
    Last Post: 01-25-2020, 05:18 PM
  4. [SOLVED] Date Format Adjustment?
    By watson150 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-15-2020, 06:15 PM
  5. [SOLVED] split formula format litrage measurement need adjustment not giving expected results
    By JEAN1972 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-17-2018, 12:49 PM
  6. Custom format to text same as custom format
    By selim69 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-21-2018, 08:12 AM
  7. select adjustment tab in cell format dialog
    By x taol in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-22-2006, 12:35 AM

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