{"id":1169,"date":"2023-06-15T07:04:08","date_gmt":"2023-06-15T07:04:08","guid":{"rendered":"https:\/\/craftcookcode.com\/?p=1169"},"modified":"2024-07-10T02:05:52","modified_gmt":"2024-07-10T02:05:52","slug":"excel-vba-concatenating-two-fields-together-with-formatting","status":"publish","type":"post","link":"https:\/\/craftcookcode.com\/?p=1169","title":{"rendered":"Excel &#038; VBA &#8211; Concatenating two fields together with formatting"},"content":{"rendered":"\n<p><\/p>\n\n\n<p>A work colleague asked me to do a &#8220;Mate&#8217;s rate&#8221; job for his team yesterday.<\/p>\n<p>They had a MS Word document with a huge table in it that they needed to convert to Excel to load into an application.<\/p>\n<p>Paraphrased&nbsp; it looked like this:<\/p>\n<style>\/*! elementor - v3.13.3 - 28-05-2023 *\/<br \/>\n.elementor-widget-image{text-align:center}.elementor-widget-image a{display:inline-block}.elementor-widget-image a img[src$=\".svg\"]{width:48px}.elementor-widget-image img{vertical-align:middle;display:inline-block}<\/style>\n<p>\t\t\t\t\t\t\t\t\t\t\t\t<img loading=\"lazy\" decoding=\"async\" width=\"740\" height=\"245\" src=\"http:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_01-1024x339.png\" alt=\"\" loading=\"lazy\" srcset=\"https:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_01-1024x339.png 1024w, https:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_01-300x99.png 300w, https:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_01-768x254.png 768w, https:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_01-1536x508.png 1536w, https:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_01.png 1916w\" sizes=\"auto, (max-width: 740px) 100vw, 740px\"><\/p>\n<p>My first thought was to copy this table out and paste it into excel to see how it looks:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"740\" height=\"387\" src=\"http:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_02-1024x535.png\" alt=\"\" loading=\"lazy\" srcset=\"https:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_02-1024x535.png 1024w, https:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_02-300x157.png 300w, https:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_02-768x401.png 768w, https:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_02.png 1205w\" sizes=\"auto, (max-width: 740px) 100vw, 740px\"><\/p>\n<p>Two problems:<\/p>\n<ol>\n<li>The cell &#8220;Some data&#8221; is merged across three rows<\/li>\n<li>Line 1, Line 2, Line 3 are now split across three rows instead of being in the one cell<\/li>\n<\/ol>\n<p>I wrote a macro to split out the merge cells; but when it came to combining cells for Line 1, Line 2 and Line 3 I ran into a problem.<\/p>\n<p>None of the usual commands and from my quick google searching could combine the cells and retained the formatting.<\/p>\n<p>So commands like:<\/p>\n<pre>=CONCAT(B2:B4)\n\n=B2 &amp; B3 &amp; B4<\/pre>\n<p>Would not retain the formatting; let alone add in line feed characters to make it a multiline cell<\/p>\n<p>I&#8217;m sure there is a better way &#8211; but strapped for time I programmed the following subroutine in VBA<\/p>\n<pre>Public Sub combineCells(cell1 As Range, cell2 As Range, output As Range)\n  &nbsp; Dim formatIndex As Long\n  &nbsp; Dim formatOffset As Long\n  &nbsp; Dim hyperlink1 As String\n&nbsp; &nbsp; Dim hyperlink2 As String\n\n    hyperlink1 = \"\"\n  &nbsp; hyperlink2 = \"\"\n\n&nbsp; &nbsp; ' Check if the cell has a hyperlin; but don't have a text version of the hyperlink in the Value 2\n  &nbsp; If cell1.Hyperlinks.Count &lt;&gt; 0 And InStr(cell1.Value2, \"https:\") &lt; 1 Then\n  &nbsp; &nbsp; &nbsp; hyperlink1 = \" [\" &amp; cell1.Hyperlinks(1).Address &amp; \"]\"\n  &nbsp; End If\n\n&nbsp; &nbsp; If cell2.Hyperlinks.Count &lt;&gt; 0 And InStr(cell2.Value2, \"https:\") &lt; 1 Then\n  &nbsp; &nbsp; &nbsp; hyperlink2 = \" [\" &amp; cell2.Hyperlinks(1).Address &amp; \"]\"\n  &nbsp; End If\n\n \n  &nbsp; ' Handling if the first cell is blank. &nbsp;If so we don't want a LF at the top of the cell\n  &nbsp; If Trim(cell1.Value2) &lt;&gt; \"\" Then\n  &nbsp; &nbsp; &nbsp; output.Value2 = cell1.Value2 &amp; hyperlink1 &amp; vbLf &amp; cell2.Value2 &amp; hyperlink2\n  &nbsp; &nbsp; &nbsp; formatOffset = Len(cell1.Value2) + Len(hyperlink1) + 1\n  &nbsp; Else\n  &nbsp; &nbsp; &nbsp; output.Value2 = cell2.Value2 &amp; hyperlink2\n  &nbsp; &nbsp; &nbsp; formatOffset = Len(cell1.Value2)\n&nbsp; &nbsp; End If\n\n  \n  &nbsp; ' Copies the formatting from cell1 to the final cell\n  &nbsp; ' You can add more options to transfer over different formatting\n  &nbsp; For formatIndex = 1 To Len(cell1.Value2)\n  &nbsp; &nbsp; &nbsp; output.Characters(formatIndex, 1).Font.Bold = cell1.Characters(formatIndex, 1).Font.Bold\n  &nbsp; &nbsp; &nbsp; output.Characters(formatIndex, 1).Font.Italic = cell1.Characters(formatIndex, 1).Font.Italic\n  &nbsp; &nbsp; &nbsp; 'output.Characters(formatIndex, 1).Font.Underline = cell1.Characters(formatIndex, 1).Font.Underline\n&nbsp; &nbsp; Next\n\n  \n  &nbsp; ' Copies the formatting from cell2 to the final cell\n  &nbsp; For formatIndex = 1 To Len(cell2.Value2)\n  &nbsp; &nbsp; &nbsp; output.Characters(formatIndex + formatOffset, 1).Font.Bold = cell2.Characters(formatIndex, 1).Font.Bold\n  &nbsp; &nbsp; &nbsp; output.Characters(formatIndex + formatOffset, 1).Font.Italic = cell2.Characters(formatIndex, 1).Font.Italic\n  &nbsp; &nbsp; &nbsp; 'output.Characters(formatIndex + formatOffset, 1).Font.Underline = cell2.Characters(formatIndex, 1).Font.Underline\n&nbsp; &nbsp; Next\n\nEnd Sub<\/pre>\n<p>Oh boy it runs slow &#8211; to combine a couple of thousands cells took half an hour and I had the typical worries that Excel crashed because everything was locked up.<\/p>\n<p>But it worked for the quick task that I was doing<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"740\" height=\"214\" src=\"http:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_03-1024x296.png\" alt=\"\" loading=\"lazy\" srcset=\"https:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_03-1024x296.png 1024w, https:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_03-300x87.png 300w, https:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_03-768x222.png 768w, https:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/06\/code_excel_concat_03.png 1308w\" sizes=\"auto, (max-width: 740px) 100vw, 740px\"><\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A work colleague asked me to do a &#8220;Mate&#8217;s rate&#8221; job for his team yesterday. They had a MS Word document with a huge table in&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[52],"tags":[],"class_list":["post-1169","post","type-post","status-publish","format-standard","hentry","category-vba-vbs"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/posts\/1169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1169"}],"version-history":[{"count":6,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/posts\/1169\/revisions"}],"predecessor-version":[{"id":1444,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/posts\/1169\/revisions\/1444"}],"wp:attachment":[{"href":"https:\/\/craftcookcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}