codevu
asked on
Macro to take date of Pivot results to Outlook msg template fields
Hi,
Is it possible for a macro to run through the results of a refreshed pivot table to take the values into an Outlook mail.
From the sample attached:
It would need to take the order ref and use the data sheet to replace the fields which are between the # signs. New rows would be needed if a Unit has more than one order in the 1 day alert stage.
One email would be needed for each Unit. Could a macro actually acheive this?
Book1.xlsx
Sample.msg
Is it possible for a macro to run through the results of a refreshed pivot table to take the values into an Outlook mail.
From the sample attached:
It would need to take the order ref and use the data sheet to replace the fields which are between the # signs. New rows would be needed if a Unit has more than one order in the 1 day alert stage.
One email would be needed for each Unit. Could a macro actually acheive this?
Book1.xlsx
Sample.msg
ASKER
I have tested it in the sample and got the same positive result - this is great. I clearly complicated things trying to work with the pivot instead of the raw data.
What happens if I add more data columns - is there a way to define particular ranges?
I'm also curious about line 28 and how it works - Test1 is coded there but how does it run through them all and replace Test1?
I am happy to add the references as needed. Not many people will use the workbook and it will be stored centrally.
What happens if I add more data columns - is there a way to define particular ranges?
I'm also curious about line 28 and how it works - Test1 is coded there but how does it run through them all and replace Test1?
I am happy to add the references as needed. Not many people will use the workbook and it will be stored centrally.
Oh darn it, I'm sorry. That line of code should've been changed. I hard coded it for testing.
This line...
Very sorry about that, my very large oversight.
We can make the range variable depending on what you want. I went by your example, so I hid certain columns and filtered what you showed. If you want other ranges, just let us know how you'd like to show it. We can do pretty much anything you'd like, so long as you can articulate it to us in detail. :)
Zack
This line...
If Evaluate("=COUNTIFS('Data'!F:F,""Team1"",'Data'!G:G,""Yes"")") > 0 Then
... should actually be changed to...If Evaluate("=COUNTIFS('Data'!F:F," & rCell.Value & ",'Data'!G:G,""Yes"")") > 0 Then
Very sorry about that, my very large oversight.
We can make the range variable depending on what you want. I went by your example, so I hid certain columns and filtered what you showed. If you want other ranges, just let us know how you'd like to show it. We can do pretty much anything you'd like, so long as you can articulate it to us in detail. :)
Zack
ASKER
Hi Zack,
Sorry about the delay -
My working sheet has about 40 columns - is it best to set each unwanted column in line 43?
Sorry about the delay -
My working sheet has about 40 columns - is it best to set each unwanted column in line 43?
codevu,
Sorry I haven't been able to respond. I'll take a look at this tonight.
Zack
Sorry I haven't been able to respond. I'll take a look at this tonight.
Zack
ASKER
Thanks Zack,
No problem, I look forward to your input
No problem, I look forward to your input
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Zack,
Thanks for this - I made the changes and have tried this on my own sheet. I appreciate my sheet may be different to the sample but I get an error when running it.
'AutoFilter method of Range class failed' on line 51.
rData.AutoFilter 50, "Yes" - my sheet has a formula which generates "Yes" or "No" based on criteria - is there any reason why I might get an error? Cell formatting? (I've tried general and text)
Any support is much appreciated.
Thanks for this - I made the changes and have tried this on my own sheet. I appreciate my sheet may be different to the sample but I get an error when running it.
'AutoFilter method of Range class failed' on line 51.
rData.AutoFilter 50, "Yes" - my sheet has a formula which generates "Yes" or "No" based on criteria - is there any reason why I might get an error? Cell formatting? (I've tried general and text)
Any support is much appreciated.
You'll usually see that error with AutoFilter if the Field value isn't set right. You're setting it at 50. So if the range started in column A, then 50 would be column AX - and it would need to be included in your range, so it would be A:AX. In this case, our code for setting the range is this...
If you need help with it, just let us know what you're setting the range to and what column you want to filter. Counting from the left, the first column in your range is going to be 'Field' 1 of the syntax.
Zack
Set rData = WS.Range("A1:G" & WS.Cells(WS.Rows.Count, 1).End(xlUp).Row)
This goes from A1 through column G, the last row being that last row in column A with data found in it. So it's A1:Gx, where the 'x' is the last (dynamic) row. You would need to change the G to AX, assuming that's your range. If your data range didn't start with A, you would need to adjust accordingly.If you need help with it, just let us know what you're setting the range to and what column you want to filter. Counting from the left, the first column in your range is going to be 'Field' 1 of the syntax.
Zack
This will not look at the PivotTable, but the data source on your Data worksheet. Please note that it does not enter an email address, nor does it automatically send the email, but rather just displays it. This code should go into a standard module of the workbook which houses this data.
Open in new window
Also please note that I did set two references here in order to use early binding. You don't have to have them, but I prefer them. If this file will move from computer to computer then I would recommend using late binding and we can adjust the code accordingly. If you try to use the code without setting the proper references then it will fail. Please look at the top of the code for the commented out code which tells which references to use, there is two of them.
Please let us know if you have any questions. The code has been tested with the supplied workbook and works for me, getting it as close to your message as possible.
HTH
Regards,
Zack Barresse