- #Vba mail merge from excel how to#
- #Vba mail merge from excel code#
- #Vba mail merge from excel download#
#Vba mail merge from excel download#
Making your own workbooks to test this example is a bit tedious, so I recommend that you download the zip-compressed example workbooks, if you want to see how it works. In this case a table with contact persons and one with company information are combined, if company names match. The last example shows how you can combine rows in 2 tables (in 2 different workbooks) based on criterion and insert the result in a new workbook. If for instance the two lists share no values, the output list will be as long as the two lists put together.
#Vba mail merge from excel code#
MsgBox Err.Description & " Procedure UniqueAndDuplicates"įor the code above to be foolproof it should verify that the two tables do exist, and it should also check if the array has more rows than the spreadsheet. Set rCell = Range("K2").Resize(UBound(vResult2), 1) MsgBox "All values are present in both tables." Set rCell = Range("J2").Resize(UBound(vResult), 1) 'swift operation instead of looping through 'define a range with the same dimensions as 'each cell we check if the value is in the 'We now loop through the two ranges, and for ReDim vResult2(1 To rA.Count + rB.Count, 1 To 1) ReDim vResult(1 To rA.Count + rB.Count, 1 To 1) 'the arrays to have as many rows as the two 'how many values we will find, so we just dimension 'be copied to the spreadsheet in one swift 'The values will be stored in two arrays that will 'code should of course check this, but I am lazy. 'In this example we take for granted that there 'To check for duplicate values we use the 'original tables, and the other contains values, 'The first table contains values that are in both 'Loops two tables/lists and makes two new tables. You can download a zip-compressed workbook (Excel 2003) with this and the previous example. With short lists speed doesn't matter much, but with longer ones we want to minimize looping. It counts the instances of a value in a given range.
To check if a value from one list is present in the other, we use the spreadsheet function "CountIf". The new lists are inserted in column J and K on sheet 1. The macro compares the two lists and makes two new: One with shared values (present in both lists) and one with non-shared values. Like the previous macro this one requires a vertical list starting in cell A1 on both sheet 1 and 2. MsgBox Err.Description & " Procedure MergeLists"įind shared and not shared values in 2 lists
'doesn't work with your Excel version then change it. 'Insert the merged list with unique values: 'duplicate value is added as key, it triggers an 'Now we add all values to our collection. 'have only one column, but several columns If for instance list 1 and 2 are like below, the output list will be like the third:ĭim colMerge As New Collection 'Collection Even if there are shared values in the 2 lists, the output list will have no duplicates. The code merges the values from 2 lists to 1 sorted list. You can also download a zip-compressed workbook (Excel 2003) with this and the next example. Copy the code by selecting it with the mouse, press CTRL+C to copy and paste into a VBA module with CTRL+V. This example requires some values (text or numbers) in cell A1 and down on sheet 1 and 2. The examples use arrays, ranges, collections, the worksheet function "CountIf" and loops. The new, combined table is put in a new workbook. The third example shows how you can combine rows in 2 tables (in 2 different workbooks) if they have a shared value or "key" - in this case a company name. In the second example we loop 2 lists and make 2 new: One with the shared values and one with the values that are not shared. The new table is put in a new workbook and sorted. In the first example 2 tables/lists are merged to 1 with no duplicates. You can also download workbooks with the examples.
#Vba mail merge from excel how to#
This page shows examples of how to merge and combine data and tables (lists) using criteria. Merge or combine data and tables with Excel VBA macros