Limit user selected range

Ben AFF

Board Regular
Joined
Sep 21, 2023
Messages
54
Office Version
  1. 365
Platform
  1. Windows
Hi, I have a Macro that allows the user to send email based on selection of rows on a spreadsheet.
I want to limit the range within which the user selection is valid to columns A:B only.
Please can you help me? Thank you.

VBA Code:
Sub ExcelToOutlookSR()
Dim mApp As Object
Dim mMail As Object
Dim SendToMail As String
Dim MailSubject As String
Dim mMailBody As String
For Each r In Selection
SendToMail = Range("M" & r.Row)
MailSubject = Range("K" & r.Row)
mMailBody = Range("L" & r.Row)
Set mApp = CreateObject("Outlook.Application")
Set mMail = mApp.CreateItem(0)
With mMail
.To = SendToMail
.Subject = MailSubject
.Body = mMailBody
.Display
End With
Next r
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Question:
Why do you allow users to select col B? I don't see the code will do anything different if for example they select A2:A3 or B2:B3.
 
Upvote 0
Question:
Why do you allow users to select col B? I don't see the code will do anything different if for example they select A2:A3 or B2:B3.
In fact you are right Akuini. Initially I thought to allow users only use A:B because its where the Purchase Order information and the item number are saved. But you where right that it would generate 2 mails per row. So I only need to have the user select Column A but consolidate in a single mail based on column B. Then in column L is where I stored the mail body for each item.

PS: can I buy you a beer or coffee :)?

Purchase Order NumberPurchase Order ItemMail Body
ABC01Purchase Order Number ABC Item 01
ABC02Purchase Order Number ABC Item 02
DEF01
Purchase Order Number DEF Item 01
DEF02
Purchase Order Number DEF Item 02
 
Upvote 0
What should happen if user select A2? Should the mailBody also get value from L4 because B2 & B4 have the same value?
 
Upvote 0
What should happen if user select A2? Should the mailBody also get value from L4 because B2 & B4 have the same value?
Thank you Akuini,

No, in that scenario,
If the user select A2, then the mail body would get values from L1 & L2
If the user select A1, then the mail body would get values from L1 & L2
...
The grouping of should happen based on repeat values in column A.
 
Upvote 0
Ok, try this:
The criteria:
User can only select range with data in Column A & consolidate in a single mail based on duplicate values in column A & get values from col L to populate the mMailBody.

VBA Code:
Sub ExcelToOutlookSR_2()
Dim mApp As Object
Dim mMail As Object
Dim SendToMail As String
Dim MailSubject As String
Dim mMailBody As String
Dim r As Range, n As Long
Dim d As Object, f As Object
Dim tx As String
Dim x, ary, g

If Not Intersect(Selection, Range("A:A")) Is Nothing And Intersect(Selection, Range("B:XFD")) Is Nothing Then

    n = Range("A" & Rows.Count).End(xlUp).Row 'get last row with data in col A
    If Not Intersect(Selection, Range("A2:A" & n)) Is Nothing Then
        Set d = CreateObject("scripting.dictionary"):        d.CompareMode = vbTextCompare
        Set f = CreateObject("scripting.dictionary"):        f.CompareMode = vbTextCompare
        
        For Each r In Intersect(Selection, Range("A2:A" & n))
            f(r.Value) = Empty
        Next
        
        For Each r In Range("A2:A" & n)
            tx = r.Value
            If f.Exists(tx) Then
                If Not d.Exists(tx) Then
                    d(tx) = r.Row
                Else
                    d(tx) = d(tx) & " " & r.Row
                End If
            End If
        Next
       
        For Each x In d
            ary = Split(d.Item(x), " ")
            SendToMail = Range("M" & ary(0))
            MailSubject = Range("K" & ary(0))
            tx = ""
            For Each g In ary
                tx = tx & vbLf & Range("L" & g)
            Next
            mMailBody = Mid(tx, 2)
'            Debug.Print mMailBody
            Set mApp = CreateObject("Outlook.Application")
            Set mMail = mApp.CreateItem(0)
            With mMail
                .To = SendToMail
                .Subject = MailSubject
                .Body = mMailBody
                .Display
            End With
        Next
    Else
        MsgBox "Please, select cells with data in column A only."
    End If
Else
    MsgBox "Please, select cells with data in column A only."
End If

End Sub
 
Upvote 0
Ok, try this:
The criteria:
User can only select range with data in Column A & consolidate in a single mail based on duplicate values in column A & get values from col L to populate the mMailBody.

VBA Code:
Sub ExcelToOutlookSR_2()
Dim mApp As Object
Dim mMail As Object
Dim SendToMail As String
Dim MailSubject As String
Dim mMailBody As String
Dim r As Range, n As Long
Dim d As Object, f As Object
Dim tx As String
Dim x, ary, g

If Not Intersect(Selection, Range("A:A")) Is Nothing And Intersect(Selection, Range("B:XFD")) Is Nothing Then

    n = Range("A" & Rows.Count).End(xlUp).Row 'get last row with data in col A
    If Not Intersect(Selection, Range("A2:A" & n)) Is Nothing Then
        Set d = CreateObject("scripting.dictionary"):        d.CompareMode = vbTextCompare
        Set f = CreateObject("scripting.dictionary"):        f.CompareMode = vbTextCompare
       
        For Each r In Intersect(Selection, Range("A2:A" & n))
            f(r.Value) = Empty
        Next
       
        For Each r In Range("A2:A" & n)
            tx = r.Value
            If f.Exists(tx) Then
                If Not d.Exists(tx) Then
                    d(tx) = r.Row
                Else
                    d(tx) = d(tx) & " " & r.Row
                End If
            End If
        Next
      
        For Each x In d
            ary = Split(d.Item(x), " ")
            SendToMail = Range("M" & ary(0))
            MailSubject = Range("K" & ary(0))
            tx = ""
            For Each g In ary
                tx = tx & vbLf & Range("L" & g)
            Next
            mMailBody = Mid(tx, 2)
'            Debug.Print mMailBody
            Set mApp = CreateObject("Outlook.Application")
            Set mMail = mApp.CreateItem(0)
            With mMail
                .To = SendToMail
                .Subject = MailSubject
                .Body = mMailBody
                .Display
            End With
        Next
    Else
        MsgBox "Please, select cells with data in column A only."
    End If
Else
    MsgBox "Please, select cells with data in column A only."
End If

End Sub
Thank you so much Akuini, it works perfect. I dont know how to thank you, wish I could by you a coffee or beer :)
 
Upvote 0

Forum statistics

Threads
1,216,128
Messages
6,129,033
Members
449,482
Latest member
al mugheen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top