Limit user selected range

Ben AFF

Board Regular
Joined
Sep 21, 2023
Messages
55
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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
you can try this
VBA Code:
Sub ExcelToOutlookSR()
    Dim r As Range
    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
        If r.Column = 1 Or r.Column = 2 Then  'limit selection range
            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
        Else
            MsgBox "invalid selection address."
        End If
    Next r
End Sub
 
Upvote 0
When you allow users to select the range, it could end up messy. For example, a user could select the entire column A and then run the code, which would cause the code to loop through all the cells in the column. So, I've modified the code to process only within the data range. In this part:
Rich (BB code):
    n = Range("A" & Rows.Count).End(xlUp).Row 'get last row with data in col A
    For Each r In Selection.Columns(1).Cells
        If r.Row > n Then Exit For  'if the selection include rows below the last row with data in col A then exit

In your original code, if user selects col A:B at once then each selected row will be processed twice which I don't think that's what you want. So, I modified the code to process only within 1 column, in this part:
For Each r In Selection.Columns(1).Cells
Maybe this:
VBA Code:
Sub ExcelToOutlookSR()
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

If Not Intersect(Selection, Range("A:B")) Is Nothing And Intersect(Selection, Range("C:XFD")) Is Nothing Then
    n = Range("A" & Rows.Count).End(xlUp).Row 'get last row with data in col A
    For Each r In Selection.Columns(1).Cells
        If r.Row > n Then Exit For  'if the selection include rows below the last row with data in col A then exit
        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
Else
    MsgBox "Please, select cells in column A or B"
End If
End Sub
 
Upvote 0
When you allow users to select the range, it could end up messy. For example, a user could select the entire column A and then run the code, which would cause the code to loop through all the cells in the column. So, I've modified the code to process only within the data range. In this part:
Rich (BB code):
    n = Range("A" & Rows.Count).End(xlUp).Row 'get last row with data in col A
    For Each r In Selection.Columns(1).Cells
        If r.Row > n Then Exit For  'if the selection include rows below the last row with data in col A then exit

In your original code, if user selects col A:B at once then each selected row will be processed twice which I don't think that's what you want. So, I modified the code to process only within 1 column, in this part:
For Each r In Selection.Columns(1).Cells
Maybe this:
VBA Code:
Sub ExcelToOutlookSR()
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

If Not Intersect(Selection, Range("A:B")) Is Nothing And Intersect(Selection, Range("C:XFD")) Is Nothing Then
    n = Range("A" & Rows.Count).End(xlUp).Row 'get last row with data in col A
    For Each r In Selection.Columns(1).Cells
        If r.Row > n Then Exit For  'if the selection include rows below the last row with data in col A then exit
        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
Else
    MsgBox "Please, select cells in column A or B"
End If
End Sub
Thank you so much Akuini, this is perfect the only problem is that when I action the macro, nothing happens? (the mails are not displayed)
 
Upvote 0
Thank you so much Akuini, this is perfect the only problem is that when I action the macro, nothing happens? (the mails are not displayed)
When you used your original code, did it work?
 
Upvote 0
When you used your original code, did it work?
Sorry Akuini it works now, not sure when I tried first it did not :). Please one more help if you are so kind, I want to consolidate in a single mail all the rows when the value repeat in column A, is this possible you help me please? Thank you.
 
Upvote 0
Do you mean you have duplicate values in col A?
And when the duplicate values are selected then you want to sent only 1 mail?
So, the mMailBody will consist of values from multiple rows in col L? in this part mMailBody = Range("L" & r.Row)
 
Upvote 0
Do you mean you have duplicate values in col A?
And when the duplicate values are selected then you want to sent only 1 mail?
So, the mMailBody will consist of values from multiple rows in col L? in this part mMailBody = Range("L" & r.Row)
Yes, precisely.
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,636
Members
452,662
Latest member
Aman1997

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