Friday, July 22, 2016

Turning Inches into Feet and Inches -- even fractions of an inch...

Turning inches into feet is simple math... but why not have a function that does it and the output looks better than if I did it by hand.   Given an integer that represents inches this little Excel VBA function will output feet and inches.

=====================code below===============================
Public Function IntoFeet(ininches As Double) As String
Dim feet As Integer
Dim inches As Double
    feet = Int(ininches / 12)
    inches = ininches - (feet * 12)
    IntoFeet = feet & "' " & Trim(Excel.WorksheetFunction.Text(inches, "#-?/?")) & """" 'formats thte output including fractions
End Function
=================================================================


I'm sure there are a lot of things I can do to improve this function, but if you have some ideas, drop me a comment!

Thanks-

Wednesday, July 20, 2016

Super Tools for Learning Regular Expression

Today I'm not going to give you any function code, rather I'm going to give you a list of resources.  Places I've collected that are a good reference for learning and using regular expression.


Sites on Regular Expressions
regular-experssions.info 
RegExp


User Guides
Wikipedia Overview - Good Starting place to learn anything
Microsoft on Regular Expression
Grymoire Navigation
Oracle Java on Regular Expression
zytrax Regular Expression User Guide

Guide to Regular Expression
Excel and Regular Expression
Mozilla and Regular Expressions Mostly Java but the principals are the same


Learning
code project 30-minute tutorial
W3C Schools  - They teach it in Java, but the most of the principles apply to Excel and VBA



Tools for Generating RE
The best Regular Expression Generating tool online - I love this tool,  put in the text expression - and it will help you break it down into it's parts, while generating the code!   Very cool!


Testing your Regular Expression:
https://regex101.com/
Desinger 215 RegExp Tester
RegEx Evaluator



Tuesday, July 19, 2016

Find Using Regular Expression in Excel - New Fuction

I've found that regular expression is one of the most useful tools in data manipulation. Being able to pass a regular expression rule and return the first instance of that expression is a powerful function.    I'm not going to get into depth on how regular expression works, there are tons of great resources on line that can help you learn how to write the expressions.    I might to into some depth later on other tools I use online, but here I'm just going to give you the VBA function that will expose the power of regular expression into your excel spreadsheet.  

Here is the code:
'-----------------------------------------------FRE---------------------------------------
Public Function FRE(StringIn As String, strPattern As String) As String
  Dim r As RegExp
    Set r = New RegExp
      With r
            .Global = True
            .MultiLine = False
            .IgnoreCase = True
            .Pattern = strPattern
        End With
   Set m = r.Execute(StringIn)
   FRE = Trim(m(0))
End Function
'------------------------------------------------------------------------------------------------

I named this function FRE - simply because I use it a ton, and didn't want to type some totally expressive name, - FRE is Find using Regular Expression.

There are two parts - the Stringin - which is the string your searching in, and strPattern which is the regular Expression Pattern.   If this function finds anything, it's only going to return the first instance.   I'm sure we can write better functions for returning arrays, making ranges, etc.   This is just dirt simple. 

Here is an example of how you could use this function -  you have a cell that has a lot of text mixed with phone numbers, but you just want to return the phone numbers from that cell, this function will return the first phone number.

Lets Say A1 = "Joe Smith (456) 456-8456 josmith@google.com"
to return the phone number, the function would look like this FRE(A1,"(\(\d{3}\).*\d{3}.*\d{4})")

And the return would be - (456) 456-8456


Super Simple.    I hope this will help somebody out there!