2011-10-20

Excel Code: VB ReturnFirst/Last and SuperTrim

Reference: These Excel/VB macros for ReturnFirstWord, ReturnLastWord and SuperTrim are very handy.  Download these routines, free of charge, without registration, from Keyliner's Public GDrive site.  Details on how to use these functions can be found in other Keyliner articles.  This article was streamlined and re-published.

Contents:
=ReturnFirstWord
=ReturnLastWord

=FindLastSpacePosition (numeric result)
=SuperTrim; Trims multiple redundant leading, trailing *and* interior spaces

=CountWords

These Excel UDF Macros (User Defined Functions) can be downloaded and attached to a speadsheet in a matter of minutes.  Once in place, they can be used in any Excel formula.  Other Keyliner routines, such as Excel CityStateZip Parse Functions use these modules.
 

Installation and Use

Source-code can be downloaded from the Keyliner public GDrive site  (formerly, this article listed the code, but the code became too ungainly to post in a blog).  You are welcome to use this code in your personal or commercial projects.

1.  Download Excel Macros from link:
(You do not need to register as a user to download)
Keyliner' Public GDrive


Download:
A800_UtilStrings.bas


2. Save the downloaded files to any directory, such as:
C:\Data\Source\CommonVB\A800_UtilStrings.bas


3. Launch Excel and Enable Excel Macros.

Microsoft considers all macros a security risk and this can be a nuisance.
Do the following to enable the macros.
  • Launch Excel; click the Orb (formerly the File Menu); choose Excel Options
  • Click Trust Center, Trust Center Settings
  • Macro Settings
  • Enable All Macros
  • Older versions of Office have similar settings.

4.  Launch Excel to a blank sheet and import the VB code using these standard Excel steps:

Click the "orb", Excel Options. 

On the left, choose "Popular"
Click [x] Show Developer tab in the ribbon

5.  Press Alt-F11 (Starts the VBA / Macro Editor)

6. On the tree-diagram, illustrated below, select your sheet ("Book1"), then:
  • Other-Mouse-Click (Book1),
  • Choose "Import File"
  • Browse and select the previously-saved module, e.g. C:\Data\Source\CommonVB\A800_UtilStrings.bas
  • You will see the code appear under the "Modules" folder (not yet in the illustration below but will be when you complete these steps)
7. Close the Visual Basic editor and return the original sheet; clicking the big "X".




Testing:

A. In Cell A1, type an example string, with leading, trailing and extra internal spaces. Press Enter when done:

example: "  Now is    the time "


B.  In cell B1, type this formula:

=SuperTrim(A1)

Results:  "Now is the time"  (with interior spaces trimmed)

C.  In cell B2, type this formula:

=ReturnLastWord(A1)


Results:  "time"


Related Keyliner Articles:
Excel UDF (User Defined Functions)

Excel VLookup - a complete tutorial
Excel Coloring Alternate Rows
Excel Parsing City-State-Zip
Excel Importing Text with Leading Zeroes
Using VBA to Send Email

2011-10-19

Salutation List Reference

Reference list for Salutations (English)

Prefixes, salutations or honorifics, such as "Dear Rt Hon Mr." John Q. Smith,

I had a request to parse prefix Salutations from a database of names and had troubles finding a comprehensive list.   Below is that list, along with links on how I did the parsing.

Shorter, common salutations are punctuated with and without periods while longer ones, by convention, are listed without punctuation (e.g. Rt Hon Mr  vs  Rt. Hon. Mr.). The referenced parsing routines use this list and the logic is easily changed for additional entries.

Leave an un-registered comment if you have additional salutations.  I purposely dropped the Pope and the Queen, figuring we would never need to write to them using a database and mail-merge.

Related articles:
Parsing First and Last Names in Excel
Parsing Salutation in Excel (Parses this list)

Parsing City State Zipcodes in Excel
Excel User-defined formulas

English Prefixed Salutations

Admiral
Air Cdre
Air Commodore
Air Marshal
Amb
Amb.
Ambassador
Baron
Brig
Brig Gen
Brig General
Brig.
Brigadier
Brigadier General
Bro
Bro.
Brother
Canon
Capt
Capt.
Captain
Cardinal
Cdr
Cdr.
Chief
Chief Warrant Officer
Cik
Cik.
Cmdr
Cmdr.
Col
Col.
Colonel
Commandant
Commander
Commissioner
Commodore
Congressman
Corporal
Cpl
Cpl.
Cpt.
Cpt
Dr
Dr.
Dr and Mrs
Dr. and Mrs.
Dr & Mrs
Dr & Mrs.
Father
First Lieutenant
First Officer
Flt Lieut
Fr.
Gen
Gen.
Generaal
General
Gov.
Governor
Group Captain
Grp Capt
Hon
Hon.
Honorable
Hr
Hr.
Judge
Justice
Lieut
Lieut Cdr
Lieut Col
Lieut Gen
Lieut.
Lieutenant Colonel
Lieutenant General
Lt Col
Lt Col.
Lt.
Maj Gen
Maj.
Major
Major General
Master Warrant Officer
Mayor
Miss
Mr
Mr.
Mrs
Mrs.
Ms
Ms.
Pr
Pr.
Pastor
President
Private
Prof
Prof Dr
Prof Dr.
Prof Sir
Prof.
Professor
Pvt
Pvt.
Rabbi
Rep.
Representative
Rev
Rev Canon
Rev Dr
Rev Mother
Rev.
Reverend
Rt Hon
Rt Hon Mr
Rt Hon Mr.
Rt Hon Ms
Rt Hon Ms.
Rt Hon Mrs
Rt Hon Mrs.
Rt Hon.
Rva
Sec.
Second Lieutenant
Secretary
Sen
Sen.
Senator
Sergeant
Sergeant
Sgt.
Sister
Sr
Sr D
Sr.
Sra
Srta
Sultan
Tan Sri
Tan Sri Dato
The Hon Dr
The Hon Dr.
The Hon Justice
The Hon Miss
The Hon Mr
The Hon Mr.
The Hon Mrs
The Hon Mrs.
The Hon Ms
The Hon Ms.
The Hon Sir
The Honorable
The Most Reverend
The Reverend
The Venerable
The Very Rev
The Very Rev.
The Very Reverend
Vice Admiral
Warrant Officer

2011-10-16

Excel - Macro Editor Removes Spaces

How To: The Excel 2007 Macro Editor is automatically removing spaces between words while typing code.

Problem: 
While typing a new function name or other text in the Excel Visual Basic Macro, the editor  is removing the spaces between words as you type (automatically, unexpectedly, auto removing, auto-correcting).  For example, you may be attempting to type a new function name:

Public Function MyFunction()
but the editor will correct the text as "PublicFunctionMyFunction()"



Note: You have also likely installed Microsoft Visual Studio 2010 sometime previously.  This product introduced a Load Test Report Add-in that is apparently causing this bug.  This article is current as-of Office 2007, SP2.  I suspect Office 2010 exhibits the same issue.


Solution:

1.   De-activate the Excel "Load Test Report Addin"  (add-in) using these steps:

a.  Click the Excel "orb" (File menu)
b.  Click the bottom-right button, "Excel Options"
c.  Click left-side "Add-Ins" menu
d.  In bottom, center pull-down, Choose "Manage" "COM Add-ins".  Click Go


2.  Uncheck [ ] "Load Test Report Addin"




3.  Press Alt-F11 to return to the Macro editor.  The problem should be resolved.

Making this change is permanent (for this workstation) and the change sticks for the current worksheet and all new and existing sheets.

Keywords:
VB VBA Editor auto-delete auto-deletes auto delete spaces at the end of the line, spaces as typing, spaces while typing, removes spaces, collapses spaces

2011-10-11

Excel - Enable Macro Developer Options

How To:  Excel Developer Options - How to enable Macro and Macro Editing for Excel 2007/2010/2013.  This is an excerpt from a previous article

1. Launch Excel and Enable Excel Macros.

Microsoft considers all macros a security risk and this can be a nuisance.
Do the following to enable the macros.  This is a one-time step for all macros.

  • Launch Excel; click the Orb (formerly the File Menu); choose Excel Options
  • Click Trust Center, Trust Center Settings
  • Macro Settings
  • Enable All Macros
  • Older versions of Office have similar settings.

2.  Expose Developer Options

Office 2008
a.  Click the "orb" (or File Menu), Excel Options 
b.  On the left, choose "Popular"
c.  Click [x] Show Developer tab in the ribbon
d.  Click OK and return to the sheet

Office 2010
a.  Click File, Excel Options, "Customize Ribbon"
b.  In the second-column ("Customize Ribbon: Main Tabs"), check [x] Developer
c.  Click OK and return to the sheet


3.  Enable Option Explicit Variables
In Office 2010 and above:
From any Blank Sheet, press Alt-F11 to open the VBA Editor

a.   Select Menu "Tools", Options, [Editor Tab],
b.  Check [x] Require Variable Declaration

This replaces the "Option Explicit" line you used to have to type in macros -- forcing you to declare variables before using - a recommended practice.

Related Posts:
Parsing First and Last Names in Excel
Parsing City State Zip in Excel
SuperTrim in Excel
User Defined Functions in Excel