• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Advance Filter in sheet2 to move few column from sheet1 in difference order

Jagdev Singh

Active Member
Hi Experts

I have to pull Name wise data from sheet1 to sheet2 (Advance filter), but in particular sequence available in sheet2. Please find the attached damo file for your reference.

Regards,
JD
 

Attachments

  • sample123.xlsx
    14.5 KB · Views: 0
In M1 of Sheet1, the header is messed up. Cell's value is:
" DueDate"

with leading spaces. Need to either remove these spaces to match Sheet2, or copy this value to Sheet2, and then AdvFilter will work.
 
Hi Luke

Thanks for your point. How can we call specific column in specific order in sheet2. Could you please help me with the code to do that.

Regards,
JD
 
Whatever order you list the column headers in sheet 2 is the order they will appear.

Is your question that you want a macro to run the AdvFilter, or just how to get the filter working?
 
Hi Luke

Sorry was confused with the concept of advance filter. I managed to get it workable. Thanks for your assistances.

I want to know is it possible to paste the table of the sheet2 in the body of the new mail in the outlook. I want to send this data to someone via mail.

Regards,

JD
 
I'd start by doing exactly what you describe, copy the range of cells, open a new email, and paste is in. It should copy as a table object into the email.

If you want to do this via Macro, check out Ron's tutorials here:
http://www.rondebruin.nl/win/section1.htm

Ron has literally written a book on emailing via Excel.
 
Hi Luke

I am trying to create a macro with the help of advance filter which will filter specific data depends upon name and date criteria. The dates between so and so and name should reflect the data in Sheet2 and from there these data goes into the mail body of the new mail (OutLook). Good you please help me to reach 2 more objectives

1 – In the criteria from date is working fine, but to date is not working, please let me know what I am doing wrong here.

2 – I was thinking this it possible to make this filter the date and name and then send an email task automate without filtering it in a separate sheet and then create a mail.

Is it possible to add a dates from and to in a sheet and vba checks the dates and create the mail for the entries automatically

Regards,

JD
 

Attachments

  • Email_macro_Final_Updated_Version.xlsb
    96.8 KB · Views: 1
Hi Jagdev,

For the dates, need to have user input a date naturally, and then your criteria range uses a formula to concatenate the "<" and ">" symbols. Big thing here is that the date needs to get converted into the long number that XL uses to represent a date. With the slash marks in there, it gets confused. :(
Filter appears to be working now.

For #2, I'm not sure what you're asking. You could call the mail macro from the first if you wanted, so there only needs to be one click...but I'd suggest leaving the process as is, as it allows the user to review what information they are about to email out.
 

Attachments

  • Email_macro_Final_Updated_Version LM.xlsb
    96 KB · Views: 2
Hi Luke

Thanks for sharing your views with me.

About the second query, I am confused what to do. The reason for my worry is the count of name in the RAW data sheet The count is near to 9000 names in reality. I am just thinking is, it possible where the user will get to know the list of names in a new sheet whose dead line is between the start and new dates so that, they know whom to send the mail and they can easily scroll the name in sheet2 and shoot the mail. Please let me know your view on it.

Regards,

JD
 
Thinking this out...
What we want then is a unique list of all the people, who have items that fall within the date in question, yes?

See the attached. I wrote a small macro that first extracts all unique names based on the dates given. Then, I wrote MainMacro which gets this list, and also prepares email for each person. This should be what you're looking for. Email is currently set to be previewed, but you could modify the MainMacro the be no preview if desired.
 

Attachments

  • Email_macro_Final_Updated_Version LM2.xlsb
    100 KB · Views: 2
Hi Luke

I check the above macro and it is working fine. Sorry for troubling you with one more request. I want to attach multiple file with the mail in case if same name customer will have multiple entries

I will be saving the entire attached document in a common folder. The naming convention of each doc file is the combination of name and Ref number. We have the attachment code in the macro.

Currently it is only performing function for one row. Is it possible it checks for all the entries of a particular mail and if PDF is available in the common location or even if one file is available from the list attach it and if none of the file from the list is available throws a pop-up msg. Which it is doing currently.

Code:
The code - FilePath = .Cells(2, "J").Value & Cells(2, "D").Value & " - " & .Cells(2, "A").Value & ".pdf"
If FileExists(FilePath) Then
              .Attachments.Add FilePath
            Else
              MsgBox "The file " & FilePath & " does not exist at that location."
            End If
Public Function FileExists(ByVal Filename As String) As Boolean
Dim lngAttr As Long
    On Error GoTo NoFile
    lngAttr = GetAttr(Filename)
    If (lngAttr And vbDirectory) <> vbDirectory Then
        FileExists = True
    End If
NoFile:
    Exit Function
End Function
 
First, note that your named range "MailMerge" is not dynamic, and probably should so that it covers all of the info returned from the filter.

Looking at the code, you will probably need to move the loop toward the end of the mail macro, so that it loops through and attaches files.
Code:
            'No change here, just to show where we are in code
            With OutMail
                .To = EmailTo
                .CC = CCto
                .BCC = ""
                .Subject = Subj
                .BodyFormat = 2
                Set olInsp = .GetInspector
                Set wdDoc = olInsp.WordEditor
                Set wdRng = wdDoc.Range(0, 0)
                wdRng.Text = StrBody
                wdRng.collapse 0
                wdRng.Paste
                wdRng.collapse 0
                wdRng.Text = StrBody1
               
             'Not sure where file names are coming from...but some sort of loop here
            For i = 2 To Range("MergeRecords").Rows.Count
                FilePath = Range("MergeRecords").Cells(i, "I").Value & Range("MergeRecords").Cells(i, "A").Value & ".pdf"
               
                If FileExists(FilePath) Then
                   .Attachments.Add FilePath
                Else
                   MsgBox "The file " & FilePath & " does not exist at that location."
                End If
            Next i
           
            .Display
 
Hi Luke

I have added MergeRecord before in the code, when I want to sent one mail with one entry at a time. It was a counter for the list of entires available in the Raw_data. I am not sure if that will be of any help here.

Regards,
JD
 
I'm not sure either...somehow you need to relate where the file path location is. With the sample file you gave me, that was not readily discernable. I was just showing where the code syntax needed to be.
 
Hi Luke,

Thanks for the above code. I somehow managed to make it run in the way I want it with the following amendment. You are start Mr. Luke

Code:
LastRow = Worksheets("Sheet2").Cells(Rows.Count, "B").End(xlUp).Row
For i = 2 To LastRow
                FilePath = Sheet2.Cells(i, "J").Value & Sheet2.Cells(i, "D").Value & " - " & Sheet2.Cells(i, "A").Value & ".pdf"
               
            If FileExists(FilePath) Then
              .Attachments.Add FilePath
            Else
              MsgBox "The file " & FilePath & " does not exist at that location."
            End If
            Next i
 
Hi Luke

I am looking to tweak this macro instead of sending all the mail on a click is it possible to generate single email at a time on a click.

We have to tweak the main macro. I tried few things like Exit for, but I think that not going to work here.

Code:
Sub MainMacro()
Dim c As Range
Dim rngNames As Range
Dim LastRow As Long

Application.ScreenUpdating = False
'First, get all the names
Call GetUniqueNames
With Worksheets("Unique Names")
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    'Verify that there is as least one record. This would be in row 2
    If LastRow = 1 Then
        MsgBox "No values found in that date range"
        Exit Sub
    Else
        Set rngNames = .Range("A2:A" & LastRow)
    End If
End With

'If there are names, loop through them all
For Each c In rngNames
    'Setup the AdvFilter
    Worksheets("RAW_Data").Range("DR2").Value = c.Value
    'For each name, get the records, and send the email
    Call FilterData
    Call Preview
Next c
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
 
Isn't that what we started with? Where you gave it a name, and macro made a single email?

Current macro gets all the names, and makes emails for each one. If you stop after sending one email, and then call macro again, it will just get all the names again. I'm not sure I understand what the process flowpath you want.
 
Hi Luke

In the start thread we were not getting unique list it is very important part which we incorporated later in the code. In the very first thread I was pulling the name wise criteria and set dates condition. Since the volume of name is huge I find your unique list criteria very useful. With the same setting can we send one mail at a time on a click instead of all at once?

Regards,

JD
 
What happens between each click? Also, can we have the Unique Names code be separate from the main one? As in, first do one click to pull all names, then another button to step through all the found names?
 
Hi Luke

What happens between each click?

I have to check to update our system and check is the fresh mail needs to be send or not. If not which is a rare case, cancel the mail.

Also, can we have the Unique Names code be separate from the main one?

We can have a separate buttons for each task that is not the concerned

As in, first do one click to pull all names, then another button to step through all the found names?

No worries about the multiple button.

Regards,

JD
 
Wonderful news.

In that case, assign UniqueNames to a different button, and change the main macro to this:
Code:
Sub MainMacro()
Dim c As Range
Dim rngNames As Range
Dim LastRow As Long

Application.ScreenUpdating = False
'IMPORTANT
'Assumes that Unique names has already been run
With Worksheets("Unique Names")
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    'Verify that there is as least one record. This would be in row 2
   If LastRow = 1 Then
        MsgBox "No more values found in that date range"
        Exit Sub
    Else
        'Changed so that it only looks at first cell
        Set rngNames = .Range("A2:A" & LastRow).Cells(1)
    End If
End With

'If there are names, loop through them all
For Each c In rngNames
    'Setup the AdvFilter
   Worksheets("RAW_Data").Range("DR2").Value = c.Value
    'For each name, get the records, and send the email
   Call FilterData
    Call Preview
Next c
c.Delete xlShiftUp
'Remove name from list, in preparation for next name
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
 
Hi Luke

I am getting error 91 - Object Variable or with block variable not set at the following line of code.

c.Delete xlShiftUp

Regards,
JD
 
My fault, forgot the C loses scope due to the loop. Sorry about that.
Code:
Sub MainMacro()
Dim c As Range
Dim rngNames As Range
Dim LastRow As Long

Application.ScreenUpdating = False
'IMPORTANT
'Assumes that Unique names has already been run
With Worksheets("Unique Names")
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    'Verify that there is as least one record. This would be in row 2
  If LastRow = 1 Then
        MsgBox "No more values found in that date range"
        Exit Sub
    Else
        'Changed so that it only looks at first cell
       Set rngNames = .Range("A2:A" & LastRow).Cells(1)
    End If
End With

'If there are names, loop through them all

'Setup the AdvFilter
Worksheets("RAW_Data").Range("DR2").Value = rngNames.Value

Call FilterData
Call Preview

'Remove name from list, in preparation for next name
rngNames.Delete xlShiftUp

Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
 
Back
Top