HELP required - VBA code modification.

geethanjali63

New Member
Joined
Dec 7, 2015
Messages
49
Dear All MVP's,I am basic user, not much knowledge in macro. currently I am using Ron de bruin's mail send with excel row and rows.i need small change in this code,I tried but not able to complete.I will explain my 2 requirement is given in last, if required I shared the code below after my requirement points..

My file data is 300,000+ row. Important condition is per user account, 1 reminder mail only, even multiple pending data also.

1. This macro is copying complete "A" column data from the same file and inserting one new sheets and pasting.
2. then removing duplicates to reach unique. Now as per this unique value (account number) used for filtering.
3. Now back to sheet 1, and applying filter in column "A". getting complete pending data of one account number. That filtered data's (visible cells only) pasted in new workbook(temporary), then again that data copied and pasted in outlook body of the message below my greeting message in outlook mail with that account's mail id. then that temporary file deleted.

My Requirement is
A. Currently my mails going with same person name like Dear XYZ, because it is taking from my main sheet "A" column 2nd cell . I need this temporary data's "A" column second cell (account holder name) data to be my email addressing person name (Dear XYZ)

B. Outlook signature should used after the pasted data.

Module 1

Sub Send_Row_Or_Rows_1()
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim Ash As Worksheet
Dim Cws As Worksheet
Dim Rcount As Long
Dim Rnum As Long
Dim FilterRange As Range
Dim FieldNum As Integer
Dim StrBody As String
Dim myName As String

myName = Range("C2").Value

StrBody = "Dear " & myName & "," & _
"Please find the pending video information," & _ "Kindly go through the videos ASAP.
" & _ "Ron's Excel Page
" & _ "

Thank you"On Error GoTo cleanupSet OutApp = CreateObject("Outlook.Application")With Application.EnableEvents = False.ScreenUpdating = FalseEnd With'Set filter sheet, you can also use Sheets("MySheet")Set Ash = ActiveSheet'Set filter range and filter column (Column with names)Set FilterRange = Ash.Range("A1:H" & Ash.Rows.Count)FieldNum = 1 'Filter column = A because the filter range start in A'Add a worksheet for the unique list and copy the unique list in A1Set Cws = Worksheets.AddFilterRange.Columns(FieldNum).AdvancedFilter _Action:=xlFilterCopy, _CopyToRange:=Cws.Range("A1"), _CriteriaRange:="", Unique:=True'Count of the unique values + the header cellRcount = Application.WorksheetFunction.CountA(Cws.Columns(1))'If there are unique values start the loopIf Rcount >= 2 ThenFor Rnum = 2 To Rcount'Filter the FilterRange on the FieldNum columnFilterRange.AutoFilter Field:=FieldNum, _Criteria1:=Cws.Cells(Rnum, 1).Value'Look for the mail address in the MailInfo worksheetmailAddress = ""On Error Resume NextmailAddress = Application.WorksheetFunction. _VLookup(Cws.Cells(Rnum, 1).Value, _Worksheets("Sheet1").Range("A1:B" & _Worksheets("Sheet1").Rows.Count), 2, False)On Error GoTo 0If mailAddress <> "" ThenWith Ash.AutoFilter.RangeOn Error Resume NextSet rng = .SpecialCells(xlCellTypeVisible)On Error GoTo 0End WithSet OutMail = OutApp.CreateItem(0)On Error Resume NextWith OutMail.To = mailAddress.Subject = "Test mail".HTMLBody = RangetoHTML(rng).HTMLBody = StrBody & .HTMLBody.Send'Or use SendEnd WithOn Error GoTo 0Set OutMail = NothingEnd If'Close AutoFilterAsh.AutoFilterMode = FalseNext RnumEnd Ifcleanup:Set OutApp = NothingApplication.DisplayAlerts = FalseCws.DeleteApplication.DisplayAlerts = TrueWith Application.EnableEvents = True.ScreenUpdating = TrueEnd WithEnd Sub


Module 2

Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006' Working in Office 2000-2016

Dim fso As Object Dim ts As Object Dim TempFile As String Dim TempWB As Workbook TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm" 'Copy the range and create a new workbook to past the data in rng.Copy Set TempWB = Workbooks.Add(1) With TempWB.Sheets(1) .Cells(1).PasteSpecial Paste:=8 .Cells(1).PasteSpecial xlPasteValues, , False, False .Cells(1).PasteSpecial xlPasteFormats, , False, False .Cells(1).Select Application.CutCopyMode = False On Error Resume Next .DrawingObjects.Visible = True .DrawingObjects.Delete On Error GoTo 0 End With 'Publish the sheet to a htm file With TempWB.PublishObjects.Add( _ SourceType:=xlSourceRange, _ Filename:=TempFile, _ Sheet:=TempWB.Sheets(1).Name, _ Source:=TempWB.Sheets(1).UsedRange.Address, _ HtmlType:=xlHtmlStatic) .Publish (True) End With 'Read all data from the htm file into RangetoHTML Set fso = CreateObject("Scripting.FileSystemObject") Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2) RangetoHTML = ts.ReadAll ts.Close RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _ "align=left x:publishsource=") 'Close TempWB TempWB.Close savechanges:=False 'Delete the htm file we used in this function Kill TempFile Set ts = Nothing Set fso = Nothing Set TempWB = NothingEnd Function
 
Last edited:
You need to put a forward / slash after CODE the end of each.
You might still have time to still edit that particular post!
 
Last edited:
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I posted now as per your advise, but still in bold letter

I have no idea why it's pasting like that. Where are you copying the text from? Please go to the VBA Editor (ALT+F11 from Excel) and copy the VBA code from there. The VBA Editor is shown below, as an example, you should select the code from the box on the right hand side, hit CTRL +C, then post it here using CTRL+V (inside a pair of code tags)

docasmailvbeditor.png


Regards
Caleeco
 
Upvote 0
I posted now as per your advise, but still in bold letter
I don't think you read that page about code tags carefully enough. The tag at the start should look like this, which I think you did: [code]

However, the tag at the end of your code should unclude a "/" which I think you have omitted. The tag at the end of the code should look like this: [/code]

Before that is any use though, your original code needs to be formatted appropriately.
 
Upvote 0
Dear Caleeco,

I done the same, from vba editor (ALT+11). pasting time it is fine. once I click Post quick reply it is changing. By the time how to pate the image same like your post. Because i need to show one image.
 
Upvote 0
you are right sir, I will try now, and i need to show my mail format as image. How to do?. if i select image it is asking url.
 
Upvote 0
Dear ALL,
As per your advise, I am trying again to paste. if fails I will type manually here.

Module 1
Rich (BB code):
Sub Send_Row_Or_Rows_1()
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim Ash As Worksheet
Dim Cws As Worksheet
Dim Rcount As Long
Dim Rnum As Long
Dim FilterRange As Range
Dim FieldNum As Integer
Dim StrBody As String
Dim myName As String


myName = Range("C2").Value


 StrBody = "Dear " & myName & "," & _
              "Please find the pending video information," & _
              "Kindly go through the videos ASAP." & _
              "Ron's Excel Page" & _
              "Thank you"


On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")
With Application
.EnableEvents = False
.ScreenUpdating = False
End With




'Set filter sheet, you can also use Sheets("MySheet")
Set Ash = ActiveSheet




'Set filter range and filter column (Column with names)
Set FilterRange = Ash.Range("A1:H" & Ash.Rows.Count)
FieldNum = 1 'Filter column = A because the filter range start in A




'Add a worksheet for the unique list and copy the unique list in A1
Set Cws = Worksheets.Add
FilterRange.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Cws.Range("A1"), _
CriteriaRange:="", Unique:=True




'Count of the unique values + the header cell
Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))




'If there are unique values start the loop
If Rcount >= 2 Then
For Rnum = 2 To Rcount




'Filter the FilterRange on the FieldNum column
FilterRange.AutoFilter Field:=FieldNum, _
Criteria1:=Cws.Cells(Rnum, 1).Value




'Look for the mail address in the MailInfo worksheet
mailAddress = ""
On Error Resume Next
mailAddress = Application.WorksheetFunction. _
VLookup(Cws.Cells(Rnum, 1).Value, _
Worksheets("Sheet1").Range("A1:B" & _
Worksheets("Sheet1").Rows.Count), 2, False)
On Error GoTo 0




If mailAddress <> "" Then
With Ash.AutoFilter.Range
On Error Resume Next
Set rng = .SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With




Set OutMail = OutApp.CreateItem(0)




On Error Resume Next
With OutMail
.To = mailAddress
.Subject = "Test mail"
.HTMLBody = RangetoHTML(rng)
.HTMLBody = StrBody & .HTMLBody
.Display 'Or use Send
End With
On Error GoTo 0




Set OutMail = Nothing
End If




'Close AutoFilter
Ash.AutoFilterMode = False




Next Rnum
End If




cleanup:
Set OutApp = Nothing
Application.DisplayAlerts = False
Cws.Delete
Application.DisplayAlerts = True




With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub


Module 2

Rich (BB code):
Rich (BB code):
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
    


    TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"


    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With


    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With


    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.ReadAll
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")


    'Close TempWB
    TempWB.Close savechanges:=False


    'Delete the htm file we used in this function
    Kill TempFile
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 
Last edited:
Upvote 0
Dear S.H.D.O,
I posted the code with any information, based on your advice. Kindly check if any chance to help for me. I am looking for your positive reply. my requirement is, inside module one "myname" is declared as string. each mail that should be updated the cell value (A1) from module 2 data.
 
Upvote 0
Dear Caleeco, First I am asking sorry you,for pasting the code in wrong format.
I am basic user, not much knowledge in macro and currently I am using the macro got from Ron de Bruin(mail send with excel row and rows).
I need small changes in below code,I tried but not able to complete.I will explain my 2 requirement finally, and I shared the code below after my requirement points.
My file data is 300,000+ row.Everyweek we need to send the reminder mail to account holders. My important requirement is per account 1 mail only,even multiple pending row also.Reminder mail should be with pending data information.


My file have 5 columns i.e: 1.User Acccount number,2.Mail ID,3.User Name,4.Video Name,5.Current Status. Mail should compose with general greeting message hernceforth pending data column c,d,e (column a,b no need).


My below code is copying the data and pasting in the mail, from column A to last. But user name not getting updated as per pasted data.to identify easy I given step details below.




1. This (2) macro is copying complete "A" column data from the Sheet1,and inserting new sheet and pasting.
2. Then removing duplicates from sheet2 to arrive unique value.Now this unique value(account number)used for sheet1 filtering.
3. Now back to sheet 1,and applying filter in column "A" to get complete pending data of one account number.This filtered data(visible cells only) pasted in new temporary workbook,then again that data copied and then that temporary file deleted.That copied data pasted in outlook as body of the message below to my greeting message in outlook.




Upto that very perfect, My Requirement is
A. Currently all the mails going with same person name i.e: Dear XYZ. Because it is taking name details from my main sheet "C" column 2nd cell . Actually it is supposed to be from temporary data's "A" column second cell(account holder name)data to be my email addressing person name i.e: Dear XYZ, next Dear Abcd,


B. All the mails going with out my signature, end of the mail signature reuired.


Module 1
Code:
Sub Send_Row_Or_Rows_1()
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim Ash As Worksheet
Dim Cws As Worksheet
Dim Rcount As Long
Dim Rnum As Long
Dim FilterRange As Range
Dim FieldNum As Integer
Dim StrBody As String
Dim myName As String


myName = Range("C2").Value


 StrBody = "Dear " & myName & "," & _
              "Please find the pending video information," & _
              "Kindly go through the videos ASAP." & _
              "Ron's Excel Page" & _
              "Thank you"


On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")
With Application
.EnableEvents = False
.ScreenUpdating = False
End With




'Set filter sheet, you can also use Sheets("MySheet")
Set Ash = ActiveSheet




'Set filter range and filter column (Column with names)
Set FilterRange = Ash.Range("A1:H" & Ash.Rows.Count)
FieldNum = 1 'Filter column = A because the filter range start in A




'Add a worksheet for the unique list and copy the unique list in A1
Set Cws = Worksheets.Add
FilterRange.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Cws.Range("A1"), _
CriteriaRange:="", Unique:=True




'Count of the unique values + the header cell
Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))




'If there are unique values start the loop
If Rcount >= 2 Then
For Rnum = 2 To Rcount




'Filter the FilterRange on the FieldNum column
FilterRange.AutoFilter Field:=FieldNum, _
Criteria1:=Cws.Cells(Rnum, 1).Value




'Look for the mail address in the MailInfo worksheet
mailAddress = ""
On Error Resume Next
mailAddress = Application.WorksheetFunction. _
VLookup(Cws.Cells(Rnum, 1).Value, _
Worksheets("Sheet1").Range("A1:B" & _
Worksheets("Sheet1").Rows.Count), 2, False)
On Error GoTo 0




If mailAddress <> "" Then
With Ash.AutoFilter.Range
On Error Resume Next
Set rng = .SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With




Set OutMail = OutApp.CreateItem(0)




On Error Resume Next
With OutMail
.To = mailAddress
.Subject = "Test mail"
.HTMLBody = RangetoHTML(rng)
.HTMLBody = StrBody & .HTMLBody
.Display 'Or use Send
End With
On Error GoTo 0




Set OutMail = Nothing
End If




'Close AutoFilter
Ash.AutoFilterMode = False




Next Rnum
End If




cleanup:
Set OutApp = Nothing
Application.DisplayAlerts = False
Cws.Delete
Application.DisplayAlerts = True




With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Module 2
Code:
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
    


    TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"


    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With


    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With


    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.ReadAll
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")


    'Close TempWB
    TempWB.Close savechanges:=False


    'Delete the htm file we used in this function
    Kill TempFile
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 
Last edited:
Upvote 0
Dear S.H.D.O.,
Thanks for your advise. Actually I am pasting properly, but after posting it's getting converted like that. I don't know how to correct it.
Actually you were not, there are no [CODE ] [ /code] tags around what you have posted originally
 
Last edited:
Upvote 0
Actually you were not, there are no [CODE ] [ /code] tags around what you have posted originally
.. as noted previously. ;)

I don't think you read that page about code tags carefully enough. The tag at the start should look like this, which I think you did: [code]

However, the tag at the end of your code should unclude a "/" which I think you have omitted. The tag at the end of the code should look like this: [/code]
 
Upvote 0

Forum statistics

Threads
1,221,531
Messages
6,160,364
Members
451,642
Latest member
mirofa

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