Need little modification to my code. Please help in setting range instead of me making vba to select for a range...Thank you!

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi, I need help withlittle modification to my code. Please help in setting range instead of me making vba to select for a range.

from the below hilighted line i am making vba select the range then take that range to mail it...which is taking me a long to complete the process...

I want to shorten the code as well as make it run faster...

now it takes around 00:01:30 seconds...


Thanks alot for helping.
Code:
[/FONT]
[FONT=Courier New]Sub MAIL_afterALL_Checked()
Dim c As Range, myindex$, LRIN&, EMAIL1$, EMAIL2$, NAME1$
LRIN = Sheets("HOME").Range("C" & Rows.Count).End(xlUp).Row[/FONT]
[FONT=Courier New]With Application
 .DisplayAlerts = False
 .ScreenUpdating = False
End With[/FONT]

[FONT=Courier New]Run "FinalFormulas"
Run "Check_ATT_EmailADDs"
Sheets("HOME").Activate
For Each c In Sheets("HOME").Range("H3:H" & LRIN)
 If c.Value <> 0 And c.Value <> "" Then
  myindex = c.Value
  NAME1 = c.Offset(0, -5).Value
  EMAIL1 = c.Offset(0, -4).Value
  EMAIL2 = c.Offset(0, -3).Value
  Debug.Print c.Value & " " & EMAIL1 & " " & EMAIL2 & " " & NAME1
    Sheets("RAW DATA").Activate
    With Sheets("RAW DATA")
[COLOR=darkslateblue][U][B]    .Range("B2").Resize(myindex).Select[/B][/U][/COLOR]
[/FONT]
[FONT=Courier New]    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object[/FONT]
[FONT=Courier New]    Set rng = Nothing
    On Error Resume Next
    [/FONT][FONT=Courier New][B][U][COLOR=darkslateblue]Set rng = Selection.SpecialCells(xlCellTypeVisible)
[/COLOR][/U][/B]    On Error GoTo 0[/FONT]
[FONT=Courier New]    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If[/FONT]
[FONT=Courier New]    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With[/FONT]
[FONT=Courier New]    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)[/FONT]
[FONT=Courier New]    On Error Resume Next
    With OutMail
        .To = EMAIL1
        .CC = EMAIL2
        .BCC = ""
        .Subject = "My Subject line"
        .HTMLBody = RangetoHTML(rng)
        .Send   'or use .Display
    End With
    On Error GoTo 0[/FONT]
[FONT=Courier New]    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With[/FONT]
[FONT=Courier New]    Set OutMail = Nothing
    Set OutApp = Nothing
    [/FONT]
[FONT=Courier New][/FONT] 
[FONT=Courier New]    Selection.EntireRow.Delete
    End With
 End If
Next c
With Application
 .DisplayAlerts = True
 .ScreenUpdating = True
End With
End Sub
Function RangetoHTML(rng As Range)
    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"
    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
    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
    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=")
    TempWB.Close savechanges:=False
    Kill TempFile
 
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Code:
Set rng = Sheets("RAW DATA").Range("B2").Resize(myindex).SpecialCells(xlCellTypeVisible)
 
Upvote 0
AlphaD, thanks alot it i working...but can u make it run faster too?:) If possible?
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,158
Members
452,892
Latest member
yadavagiri

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