3

Hi I got a code which would filter the unique values in the A column and copy the whole range from A1:H, but I want to ignore the first column and want range to be copied form B1:H.

Eg: if there is a table with marks of students and I want to post the individual marks table to every student separately. This macro is sending the table along with the student name which is in the first column, but I need only marks table, don't need students name along with that.

Here is my 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 mailAddress As String
Dim StrBody As String

On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With


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("Mailinfo").Range("A1:B" & _
                            Worksheets("Mailinfo").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 = StrBody & RangetoHTML(rng)
                .Display  'Or use Send

                StrBody = Sheets("Body").Range("A1").Value & "<br>" & _
          Sheets("Body").Range("A2").Value & "<br>" & _
          Sheets("Body").Range("A3").Value & "<br><br><br>"

            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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Revan Erraboina
  • 155
  • 1
  • 11
  • 2
    Please don't use [tag:macros] for VBA related questions! Further more for sending notes to individuals in your list, you should definitely **use [Word's Mailing tools**](https://support.office.com/en-us/article/Mail-merge-using-an-Excel-spreadsheet-858c7d7f-5cc0-4ba1-9a7b-0a948fa3d7d3) (click it, I've linked the relevant MS thread) rather than VBA code, that is exactly what it is supposed to do! ;) – R3uK Mar 15 '17 at 10:35

2 Answers2

0

By using offset you can select specific filtered column without heading or with heading

Please have a look below code :

Set rng = .AutoFilter.Range.Offset(1, ColumnNumber).Resize(.AutoFilter.Range.Rows.Count - 1, ColumnCount).SpecialCells(xlCellTypeVisible)

ColumnNumber - start column to copy ColumnCount - number of columns to copy

Try below one :

set rng = Ash.Autofilter.Range.Offset(1).Resize(Ash.AutoFilter.Range.Rows.Count - 1,7).SpecialCells(xlCellTypeVisible)

0

If you want to stick with your solution rather than using Word's Mailing tools,

Just change this line :

Set rng = .SpecialCells(xlCellTypeVisible)

To

Set rng = Application.Intersect(.SpecialCells(xlCellTypeVisible),Ash.Range("B:H"))
R3uK
  • 14,417
  • 7
  • 43
  • 77