VBA to find a Name

radrock13

New Member
Joined
May 8, 2015
Messages
4
Hello Everyone,

I am new to MrExcel. What I am looking at doing is writing a VBA that will allow me to search for a specific name.
Once the name is found I would like to select the entire row that the name is found in.
After the cells are selected I would like to copy those cells and send them in an Email to person(s) that were selected.

I am using:
- Windows 7
- Excel 2013

INFO:

Excel Sheet1 has the data that needs to search for the name in column I. Then Sheet2 has the persons name in column A and the corresponding email in Column B.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi radrock13,

Perhaps you could give just a drop more information... If the name on Sheet2 is a match for the name on Sheet1, could we do the search against column A on Sheet2 or do you have to see the data columns on Sheet1 to decide who you want to search for. Additionally, what are the columns (letters) on Sheet1 that you want to copy.

igold
 
Upvote 0
Hi radrock13,

Perhaps you could give just a drop more information... If the name on Sheet2 is a match for the name on Sheet1, could we do the search against column A on Sheet2 or do you have to see the data columns on Sheet1 to decide who you want to search for. Additionally, what are the columns (letters) on Sheet1 that you want to copy.

igold

igold,

Basically Sheet1 is an improvement idea table. One column has the idea and a possible solution. Then another column has who it was submitted by. There are a few more columns of data but our management team will go through and assign the improvement task to an individual. That name is found on Sheet1 column "I". Then Sheet2 has the name in column "A" and the Email address in column "B". The items that need to be selected are on Sheet1 columns A-O so it would be 15 columns total. The other issue that I am having is the fact that sometimes one person is given more than one assignment. I hope that this is making sense is is a challenging one. thanks for any help.
 
Upvote 0
Hi radraock13,

See if this code does what you had in mind. You did not specify a subject line for the email so that can be added on the fly. Please remember to test the code on a backup copy of your file!

Code:
Sub CreateMail()

    Dim objOutlook As Object
    Dim objMail As Object
    Dim rngTo As Range
    Dim rngSubject As Range
    Dim rngBody As Range
    Dim rngAttach As Range
    Dim strTo As String
    Dim ws1 As Worksheet: Set ws1 = Worksheets("Sheet1")
    Dim ws2 As Worksheet: Set ws2 = Worksheets("Sheet2")
    Dim lRowsht1 As Single
    Dim lRowsht2 As Single
    Dim cRow As Single
    Dim i As Integer
    Dim strBody As String
    Dim n As Single
    Dim emL As String
    Dim NmCt As Integer
    
    strBody = ""
    NmCt = 0
     
 '||||||||||||||||||||||| Get Last Row of Data On Two Sheets |||||||||||||||||
    
    lRowsht1 = ws1.Cells(Rows.Count, "I").End(xlUp).Row
    lRowsht2 = ws2.Cells(Rows.Count, "A").End(xlUp).Row
    
'||||||||||||||||||||||| Get Name To Search For |||||||||||||||||||||||||||||||
    
    strTo = UCase(InputBox("Please Enter Name to Search For..."))
    
'||||||||||||||||||| Search Name Range Sheet1 ||||||||||||||||||||||||||||||||

    For i = 2 To lRowsht1
        If Cells(i, 9).Value = strTo Then GoTo stOver
    Next
    GoTo Err_Handler
    
'||||||||||||||||||||||| Copy Name Row For Body of Email|||||||||||||||||||||||||||||
stOver:
    If NmCt >= 1 Then strBody = ""
    cRow = i
    For i = 1 To 15
        strBody = strBody & "  " & Cells(cRow, i).Value
    Next
    NmCt = NmCt + 1
    
'||||||||||||||||||||||| Get Email Addrress From Sheet2 ||||||||||||||||||||||

    For n = 2 To lRowsht2
        If ws2.Cells(n, 1).Value = strTo Then
            emL = ws2.Cells(n, 1).Offset(0, 1).Value
            GoTo cr8ml
        End If
    Next
        
'|||||||||||||||||||||||| Search For Name Repeats ||||||||||||||||||||||||||||||
Lkagn:
    If NmCt >= 1 Then
        For i = cRow + 1 To lRowsht1
            If Cells(i, 9).Value = strTo Then GoTo stOver
        Next
    End If
End
        
'||||||||||||||||||||||| Create Email in Outlook |||||||||||||||||||||||||

cr8ml:
    Set objOutlook = CreateObject("Outlook.Application")
    Set objMail = objOutlook.CreateItem(0)

    With objMail
        '.to = rngTo.Value
        .To = emL
        '.Subject = rngSubject.Value
        '.Body = rngBody.Value
        .body = strBody
        '.Attachments.Add rngAttach.Value
        .Display                   'Instead of .Display, you can use .Send to send the email _
                                         or .Save to save a copy in the drafts folder
    End With

    Set objOutlook = Nothing
    Set objMail = Nothing
    Set rngTo = Nothing
    Set rngSubject = Nothing
    Set rngBody = Nothing
    Set rngAttach = Nothing
    
'|||||||||||||||||||||| Start Over ||||||||||||||||||||||||
    
    GoTo Lkagn
    
    End
    
Err_Handler:
If NmCt = 0 Then MsgBox "Name Not Found", vbInformation, "Name Search"

End Sub


HTH

igold
 
Upvote 0
igold,

Thank you for your help. This worked very well I had to make a few modifications but it is a great code. Thank you very much for your assistance. I am getting a mismatch error on the Body of the Email portion but it is only for a few names any idea what that could be?

Thanks again for all of your help.













Hi radraock13,

See if this code does what you had in mind. You did not specify a subject line for the email so that can be added on the fly. Please remember to test the code on a backup copy of your file!

Code:
Sub CreateMail()

    Dim objOutlook As Object
    Dim objMail As Object
    Dim rngTo As Range
    Dim rngSubject As Range
    Dim rngBody As Range
    Dim rngAttach As Range
    Dim strTo As String
    Dim ws1 As Worksheet: Set ws1 = Worksheets("Sheet1")
    Dim ws2 As Worksheet: Set ws2 = Worksheets("Sheet2")
    Dim lRowsht1 As Single
    Dim lRowsht2 As Single
    Dim cRow As Single
    Dim i As Integer
    Dim strBody As String
    Dim n As Single
    Dim emL As String
    Dim NmCt As Integer
    
    strBody = ""
    NmCt = 0
     
 '||||||||||||||||||||||| Get Last Row of Data On Two Sheets |||||||||||||||||
    
    lRowsht1 = ws1.Cells(Rows.Count, "I").End(xlUp).Row
    lRowsht2 = ws2.Cells(Rows.Count, "A").End(xlUp).Row
    
'||||||||||||||||||||||| Get Name To Search For |||||||||||||||||||||||||||||||
    
    strTo = UCase(InputBox("Please Enter Name to Search For..."))
    
'||||||||||||||||||| Search Name Range Sheet1 ||||||||||||||||||||||||||||||||

    For i = 2 To lRowsht1
        If Cells(i, 9).Value = strTo Then GoTo stOver
    Next
    GoTo Err_Handler
    
'||||||||||||||||||||||| Copy Name Row For Body of Email|||||||||||||||||||||||||||||
stOver:
    If NmCt >= 1 Then strBody = ""
    cRow = i
    For i = 1 To 15
    [COLOR=#ff0000]    strBody = strBody & "  " & Cells(cRow, i).Value "mismatch error here"[/COLOR]
    Next
    NmCt = NmCt + 1
    
'||||||||||||||||||||||| Get Email Addrress From Sheet2 ||||||||||||||||||||||

    For n = 2 To lRowsht2
        If ws2.Cells(n, 1).Value = strTo Then
            emL = ws2.Cells(n, 1).Offset(0, 1).Value
            GoTo cr8ml
        End If
    Next
        
'|||||||||||||||||||||||| Search For Name Repeats ||||||||||||||||||||||||||||||
Lkagn:
    If NmCt >= 1 Then
        For i = cRow + 1 To lRowsht1
            If Cells(i, 9).Value = strTo Then GoTo stOver
        Next
    End If
End
        
'||||||||||||||||||||||| Create Email in Outlook |||||||||||||||||||||||||

cr8ml:
    Set objOutlook = CreateObject("Outlook.Application")
    Set objMail = objOutlook.CreateItem(0)

    With objMail
        '.to = rngTo.Value
        .To = emL
        '.Subject = rngSubject.Value
        '.Body = rngBody.Value
        .body = strBody
        '.Attachments.Add rngAttach.Value
        .Display                   'Instead of .Display, you can use .Send to send the email _
                                         or .Save to save a copy in the drafts folder
    End With

    Set objOutlook = Nothing
    Set objMail = Nothing
    Set rngTo = Nothing
    Set rngSubject = Nothing
    Set rngBody = Nothing
    Set rngAttach = Nothing
    
'|||||||||||||||||||||| Start Over ||||||||||||||||||||||||
    
    GoTo Lkagn
    
    End
    
Err_Handler:
If NmCt = 0 Then MsgBox "Name Not Found", vbInformation, "Name Search"

End Sub


HTH

igold
 
Upvote 0
Hi radrock13,

I am glad that the code does what you wanted (for the most part). I tried a couple of things to get an error like you described but was unsuccessful. My first guess is that there is data in one of the cells that is being copied, that the code does not like. That part of the code is a loop. Perhaps you could step through the code and figure out which cell makes the code break. Or perhaps see if you can compare the fields for "successful" names against the "failed" names for differences in that row. I will keep trying. If you want to post a row that fails, I will look at it.

Thanks for the feedback.

igold
 
Upvote 0
igold,

Thank you again for all of your help. I was able to figure it out, and even put an "IF and" statement into it to help filter the data even more.

Thanks,

Radrock13
 
Upvote 0

Forum statistics

Threads
1,215,554
Messages
6,125,487
Members
449,233
Latest member
Deardevil

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