{"id":1826,"date":"2009-12-07T17:28:23","date_gmt":"2009-12-07T22:28:23","guid":{"rendered":"http:\/\/blogs.terrorware.com\/geoff\/?p=1826"},"modified":"2009-12-07T17:28:23","modified_gmt":"2009-12-07T22:28:23","slug":"string-mangling-in-excelvba","status":"publish","type":"post","link":"http:\/\/blogs.terrorware.com\/geoff\/2009\/12\/07\/string-mangling-in-excelvba\/","title":{"rendered":"String mangling in Excel\/VBA"},"content":{"rendered":"<p>Often, I find myself needing to do string mangling in spreadsheets. For instance, in an Excel dump of an MS Access database, the names of institutions were sometimes written as &#8220;Foo, University of&#8221; or &#8220;Foo U&#8221;. I wanted these to be formatted as &#8220;University of Foo&#8221; and &#8220;Foo University&#8221; respectively. I may be misinformed but there doesn&#8217;t seem to be regexp style substitution in VBA. I could do this directly in the spreadsheet, but conditional use of functions gets to be hard to understand and difficult to edit. So, I wrote the following custom function in VBA.<\/p>\n<div style=\"border-width: 1px\">\n<div>\n<pre>Function FormatInstitutionName(institutionName As Variant) As <span>String<\/span>\r\n'Formats an institution name <span>for<\/span> <span>import<\/span> into CiviCRM:\r\n'<span>\"Foo, University of\"<\/span> =&gt; <span>\"University of Foo\"<\/span>\r\n'<span>\"Foo U\"<\/span> =&gt; <span>\"Foo University\"<\/span>\r\n    Dim newInstitutionName As <span>String<\/span>\r\n\r\n    newInstitutionName = institutionName\r\n\r\n    If InStr(newInstitutionName, <span>\", University of\"<\/span>) Then\r\n        newInstitutionName = <span>\"University of \"<\/span> &amp; Replace(newInstitutionName, <span>\", University of\"<\/span>, \"\")\r\n    End If\r\n\r\n    If InStr(Len(newInstitutionName) - 1, newInstitutionName, <span>\"U\"<\/span>) Then\r\n        newInstitutionName = Left(newInstitutionName, Len(newInstitutionName) - 2) &amp; <span>\" University\"<\/span>\r\n    End If\r\n\r\n    FormatInstitutionName = newInstitutionName\r\nEnd Function<\/pre>\n<\/div>\n<\/div>\n<p>Add this to your workbook by following the instructions at <a rel=\"nofollow\" href=\"http:\/\/www.vertex42.com\/ExcelArticles\/user-defined-functions.html\">http:\/\/www.vertex42.com\/ExcelArticles\/user-defined-functions.html<\/a><\/p>\n<p>Then call it in your spreadsheet cells like like:<\/p>\n<div style=\"border-width: 1px\">\n<div>\n<pre>=FormatInstitutionName(C2)<\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Often, I find myself needing to do string mangling in spreadsheets. For instance, in an Excel dump of an MS Access database, the names of institutions were sometimes written as &#8220;Foo, University of&#8221; or &#8220;Foo U&#8221;. I wanted these to be formatted as &#8220;University of Foo&#8221; and &#8220;Foo University&#8221; respectively. I may be misinformed but&hellip; <a class=\"more-link\" href=\"http:\/\/blogs.terrorware.com\/geoff\/2009\/12\/07\/string-mangling-in-excelvba\/\">Continue reading <span class=\"screen-reader-text\">String mangling in Excel\/VBA<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[27,4],"tags":[774,16546,775,61,469,777,776],"class_list":["post-1826","post","type-post","status-publish","format-standard","hentry","category-hacks","category-note-to-self","tag-excel","tag-microsoft","tag-office","tag-programming","tag-replace","tag-string","tag-vba","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4wnIz-ts","_links":{"self":[{"href":"http:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts\/1826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/comments?post=1826"}],"version-history":[{"count":1,"href":"http:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts\/1826\/revisions"}],"predecessor-version":[{"id":1827,"href":"http:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts\/1826\/revisions\/1827"}],"wp:attachment":[{"href":"http:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/media?parent=1826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/categories?post=1826"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/tags?post=1826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}