Automate task in vba

plutonik33

New Member
Joined
Dec 7, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi,

I don't have vba experience, but I really need to solve the following tasks:

First table has to be filtered by unique users.
Filtered table then needs to be stored in a new document and named after the name of the user.

Plus it would be amazing if I can then send the generated documents attached as an email with subject text and additional information like "Dear User, ...." (Using Outlook).

Appreciate any help!

Here is my Mini-sheet:

Filter by column 'User'.xlsx
ABCDEFGHIJKLMNOPQRSTUV
1ReihenfolgeEndSZSM-Auftrags-nummerPosition für Google MapsStraßePLZOrtPrioTausch gegenONONKZAsBRegion_PTIMaterial-nummerJahrKalender-wocheWartungs-fensterUserWartungs-fenster DatumBeginn Wartungs-fensterEnde Wartungs-fensterIP
2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxS1xxxxxxxx
3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxS1xxxxxxxx
4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxS2xxxxxxxx
5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxS2xxxxxxxx
6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxS3xxxxxxxx
7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxS3xxxxxxxx
8xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxS4xxxxxxxx
9xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxS4xxxxxxxx
Tabelle2
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:A9Expression=#BEZUG!="nein"textNO
H2:H9Cell Value="entfällt"textNO
H2:H9Cell Value="entfällt"textNO
B2:V9Expression=#BEZUG!="nein"textNO
B1Cell ValueduplicatestextNO
 

Attachments

  • Table.PNG
    Table.PNG
    37.1 KB · Views: 6

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
What would you like to enter as the subject text and additional information? Is the email address anywhere in your data? It would be helpful if you could post an English version to help us better understand the headers.
 
Upvote 0
Hi mumps,

thank you for the quick reply! Is it possible to leave the headers as they are? Only relevant ones are 'User', 'Email' (I will add this column right next to 'User'), 'Name' (full name of user) and 'week'.

As Subject text: "Operational plan for " 'User' "-" 'Name' "-" 'Week'
Additional Information: "Dear" 'Name' ",enclosed is the operational plan for" 'Week' "."


Here the updated Minisheet (relevant columns are merked green):

Filter by column 'User'.xlsx
ABCDEFGHIJKLMNOPQRSTUVWX
2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek1xxS1user1@hotmail.comMax Mustermannxxxxxxxx
3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek1xxS1user1@hotmail.comMax Mustermannxxxxxxxx
4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek2xxS2user2@hotmail.comPeter Mustermannxxxxxxxx
5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek2xxS2user2@hotmail.comPeter Mustermannxxxxxxxx
6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek3xxS3user3@hotmail.comGreta Mustermannxxxxxxxx
7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek3xxS3user3@hotmail.comGreta Mustermannxxxxxxxx
8xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek4xxS4user4@hotmail.comTimo Mustermannxxxxxxxx
9xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek4xxS4user4@hotmail.comTimo Mustermannxxxxxxxx
Tabelle2
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:A9Expression=#BEZUG!="nein"textNO
H2:H9Cell Value="entfällt"textNO
H2:H9Cell Value="entfällt"textNO
B2:X9Expression=#BEZUG!="nein"textNO
 
Upvote 0
Filter by column 'User'.xlsx
ABCDEFGHIJKLMNOPQRSTUVWX
1ReihenfolgeEndSZSM-Auftrags-nummerPosition für Google MapsStraßePLZOrtPrioTausch gegenONONKZAsBRegion_PTIMaterial-nummerJahrWeekWartungs-fensterUserEmailNameWartungs-fenster DatumBeginn Wartungs-fensterEnde Wartungs-fensterIP
2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek1xxS1user1@hotmail.comMax Mustermannxxxxxxxx
3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek1xxS1user1@hotmail.comMax Mustermannxxxxxxxx
4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek2xxS2user2@hotmail.comPeter Mustermannxxxxxxxx
5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek2xxS2user2@hotmail.comPeter Mustermannxxxxxxxx
6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek3xxS3user3@hotmail.comGreta Mustermannxxxxxxxx
7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek3xxS3user3@hotmail.comGreta Mustermannxxxxxxxx
8xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek4xxS4user4@hotmail.comTimo Mustermannxxxxxxxx
9xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxweek4xxS4user4@hotmail.comTimo Mustermannxxxxxxxx
Tabelle2
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:A9Expression=#BEZUG!="nein"textNO
H2:H9Cell Value="entfällt"textNO
H2:H9Cell Value="entfällt"textNO
B2:X9Expression=#BEZUG!="nein"textNO
B1Cell ValueduplicatestextNO
 
Upvote 0
In order to re-name the new workbooks, they have to be saved. What is the full path to the folder where you want to save the new workbooks?
 
Upvote 0
In order to re-name the new workbooks, they have to be saved. What is the full path to the folder where you want to save the new workbooks?
For now it would be perfect if I can save the generated documents in following path "C:\Users\Documents\Tauschprojekt"
 
Upvote 0
Try:
VBA Code:
Sub CreateEmails()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, dic As Object, srcWB As Workbook, srcWS As Worksheet
    Dim OutApp As Object, OutMail As Object
    Set srcWB = ThisWorkbook
    Set srcWS = srcWB.Sheets(1)
    With srcWS
        v = .Range("R2", .Range("R" & .Rows.Count).End(xlUp)).Value
    End With
    Set dic = CreateObject("Scripting.Dictionary")
    Set OutApp = CreateObject("Outlook.Application")
    For i = 1 To UBound(v, 1)
        If Not dic.Exists(v(i, 1)) Then
            dic.Add v(i, 1), Nothing
            With srcWS
                .Range("A1").CurrentRegion.AutoFilter 18, v(i, 1)
                .AutoFilter.Range.Copy
                Workbooks.Add 1
                Range("A1").PasteSpecial
                ActiveWorkbook.SaveAs Filename:="C:\Users\Documents\Tauschprojekt\" & Range("R2") & ".xlsx", FileFormat:=51
                Set OutMail = OutApp.CreateItem(0)
                With OutMail
                    .To = Range("S2").Value
                    .Subject = "Operational plan for " & Range("S2").Value & "-" & Range("T2").Value & "-" & Range("P2").Value
                    .HTMLBody = "Dear " & Range("T2").Value & "," & "<br><br>" & "Enclosed is the operational plan for " & Range("P2").Value & "."
                    .attachments.Add ActiveWorkbook.FullName
                    .Display
                End With
                ActiveWorkbook.Close False
            End With
        End If
    Next i
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi,

my next task is very specific and I am struggling with it, because connected cells and a special paste is involved. Also because I lack vba experience.
Hope you can help...

Short Version: I have two documents and I want to copy serial numbers from one document and paste in another document with connected cells.

Detailed explanation:

1) Copy Range("A1:B1) from source "document1.xlsm" to a temporary document "temp.xlsx" in Range("A1:B1") -> Now I have the headers in "temp.xlsx".
2) Copy Range("A2:B8") from source "document1.xlsm" and paste in "temp.xlsx" underneath the headers in Range("A2:B8") and format Range("A1:B8") as a table. -> Now I have the first half of my table
3) In "temp.xlsx" copy Range("A2:B8") and paste it in underneath in Range("A9:B15") -> This is an intermediary step because I need redundant data in order to be able to paste in connected cells later on.
4) Now I sort this table by increasing number in column 'Order'


The result should look like this:

Order SerialNumber
1 49/6353/5/71E2
1 49/6353/5/71E2
2 49/6322/54/71E1
2 49/6322/54/71E1
3 49/6322/53/71E1
3 49/6322/53/71E1
4 49/6322/51/71E1
4 49/6322/51/71E1
5 49/6322/52/71E1
5 49/6322/52/71E1
6 49/6395/4/71E2
6 49/6395/4/71E2
7 49/6393/6/71E2
7 49/6393/6/71E2


This copy and paste mechanism has to be repeated for all Ranges of the table where number starts at 1 and ends one row before next number 1 starts.
To better understand what I mean, needed Ranges are seperated by thik lines.

As a result these additional tables:

Order SerialNumber
1 49/6307/9/71E2
1 49/6307/9/71E2
2 49/6307/8/71E2
2 49/6307/8/71E2
3 49/631/663/71E1
3 49/631/663/71E1
4 49/6352/33/71E2
4 49/6352/33/71E2
5 49/6353/4/71E2
5 49/6353/4/71E2
6 49/6353/9/71E2
6 49/6353/9/71E2
7 49/6353/11/71E2
7 49/6353/11/71E2
8 49/6353/13/71E2
8 49/6353/13/71E2

And

Order SerialNumber
1 49/6335/13/71EA
1 49/6335/13/71EA
2 49/6335/9/71EA
2 49/6335/9/71EA
3 49/681/854/71E2
3 49/681/854/71E2
4 49/681/235/71E2
4 49/681/235/71E2
5 49/681/922/71E2
5 49/681/922/71E2
6 49/681/252/71E2
6 49/681/252/71E2


Now that I have my tables with redundant sorted data prepared in "temp.xlsx", I have to copy the sorted Ranges one by one into "document2.xlsx"

1) Copy first sorted Range("B2:B15") and paste as formula (I have to do this because of connected cells) in connected Cell("B4:B5") (I marked this cell in green in "document2.xlsx")
2) Copy second sorted Range and paste underneath in "document2.xlsx" and so on...


The result should look like attached image "document2_result.PNG"

I hope you can follow my explanation and it would be amazing if you can help me once more!

------------------------------------------------------------------------------------------------------------
1) "document1.xlsm"

document1.xlsx
AB
1OrderSerialNumber
2149/6353/5/71E2
3249/6322/54/71E1
4349/6322/53/71E1
5449/6322/51/71E1
6549/6322/52/71E1
7649/6395/4/71E2
8749/6393/6/71E2
9149/6307/9/71E2
10249/6307/8/71E2
11349/631/663/71E1
12449/6352/33/71E2
13549/6353/4/71E2
14649/6353/9/71E2
15749/6353/11/71E2
16849/6353/13/71E2
17149/6335/13/71EA
18249/6335/9/71EA
19349/681/854/71E2
20449/681/235/71E2
21549/681/922/71E2
22649/681/252/71E2
Source
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B17:B22Expression=#BEZUG!="nein"textNO
B9:B16Expression=#BEZUG!="nein"textNO
B2:B8Expression=#BEZUG!="nein"textNO
B1Cell ValueduplicatestextNO



2) "document2.xlsm"

document2.xlsx
ABCD
1Serial Numbers
2
3
4ENDSZ:Serial Nummer alt:
5Serial Nummer neu:
6ENDSZ:Serial Nummer alt:
7Serial Nummer neu:
8ENDSZ:Serial Nummer alt:
9Serial Nummer neu:
10ENDSZ:Serial Nummer alt:
11Serial Nummer neu:
12ENDSZ:Serial Nummer alt:
13Serial Nummer neu:
14ENDSZ:Serial Nummer alt:
15Serial Nummer neu:
16ENDSZ:Serial Nummer alt:
17Serial Nummer neu:
18ENDSZ:Serial Nummer alt:
19Serial Nummer neu:
20ENDSZ:Serial Nummer alt:
21Serial Nummer neu:
22ENDSZ:Serial Nummer alt:
23Serial Nummer neu:
24ENDSZ:Serial Nummer alt:
25Serial Nummer neu:
26ENDSZ:Serial Nummer alt:
27Serial Nummer neu:
28ENDSZ:Serial Nummer alt:
29Serial Nummer neu:
30ENDSZ:Serial Nummer alt:
31Serial Nummer neu:
32ENDSZ:Serial Nummer alt:
33Serial Nummer neu:
34ENDSZ:Serial Nummer alt:
35Serial Nummer neu:
36ENDSZ:Serial Nummer alt:
37Serial Nummer neu:
38ENDSZ:Serial Nummer alt:
39Serial Nummer neu:
40ENDSZ:Serial Nummer alt:
41Serial Nummer neu:
42ENDSZ:Serial Nummer alt:
43Serial Nummer neu:
44ENDSZ:Serial Nummer alt:
45Serial Nummer neu:
46ENDSZ:Serial Nummer alt:
47Serial Nummer neu:
48ENDSZ:Serial Nummer alt:
49Serial Nummer neu:
50ENDSZ:Serial Nummer alt:
51Serial Nummer neu:
52ENDSZ:Serial Nummer alt:
53Serial Nummer neu:
54ENDSZ:Serial Nummer alt:
55Serial Nummer neu:
56ENDSZ:Serial Nummer alt:
57Serial Nummer neu:
58ENDSZ:Serial Nummer alt:
59Serial Nummer neu:
60ENDSZ:Serial Nummer alt:
61Serial Nummer neu:
62ENDSZ:Serial Nummer alt:
63Serial Nummer neu:
64ENDSZ:Serial Nummer alt:
65Serial Nummer neu:
66ENDSZ:Serial Nummer alt:
67Serial Nummer neu:
Seriennummer
 

Attachments

  • document2_result.PNG
    document2_result.PNG
    50.9 KB · Views: 6
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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