Excel Pragma

April 20, 2005

VSTO Videos and ClassicVB.org

Filed under: General — Fadi @ 7:00 am

VSTO has been there for a while. But like every other Exceler loyal to his VBA, I’ve signed the petition for classic VB , especially with the deadline on VB coming very soon (here’s a Google for more details).

Lately, my curiosity took the best of me and got me to initiate contact with the other side. so I found some interesting videos at Microsoft Visual Studio 2005 Tools for the Microsoft Office System : Video Tutorials … and my curiosity has grown even stronger.

Has any of you moved ? how was it ?

April 18, 2005

Converting Numbers to Strings with Currencies

Filed under: User-Defined Functions — Fadi @ 3:32 pm

There have been a few articles on this topic but I haven’t found any that took into consideration currencies other than US Dollars and with different smaller denominations (for example, there are 1000 fils to the Kuwaity Dinar but only 100 cents to the US Dollar).

The most recent post I saw about Converting Numbers to Words was on pdbook.com (with a link to the Microsoft KnowledgeBase Article).

So here’s my version of this UDF. It’s based on the code from pdbook.com but is recursive and accepts parameters to define (a) the name of the currency, (b) the name of the smaller denomination and (c) the decimal precision.

Function SpellNumber(ByVal strAmount As String, strCur As String, strDec As String, iPrec As Integer)
    Dim BigDenom As String, SmallDenom As String, Temp As String
    Dim iDecimalPlace As Integer
    Dim Count As Integer
    
    ReDim Place(9) As String
    Place(2) = ” Thousand “
    Place(3) = ” Million “
    Place(4) = ” Billion “
    Place(5) = ” Trillion “
    
    ‘ String representation of amount.
    strAmount = Trim(Str(strAmount))
    
    ‘ Position of decimal place 0 if none.
    iDecimalPlace = InStr(strAmount, “.”)
    
    ‘ Separate the Integer part from the decimals.
    If iDecimalPlace > 0 Then
        SmallDenom = Left(Right(strAmount, Len(strAmount) - iDecimalPlace) & “0000000000″, iPrec)
        SmallDenom = SpellNumber(SmallDenom, strDec, “”, 0)
        BigDenom = Left(strAmount, iDecimalPlace - 1)
        BigDenom = SpellNumber(BigDenom, strCur, “”, 0)
        SpellNumber = BigDenom & ” and ” & SmallDenom
        Exit Function
    End If
    If iDecimalPlace = 0 Then
        Count = 1
        Do While strAmount <> “”
            Temp = GetHundreds(Right(strAmount, 3))
            If Temp <> “” Then BigDenom = Temp & Place(Count) & BigDenom
            If Len(strAmount) > 3 Then
                strAmount = Left(strAmount, Len(strAmount) - 3)
            Else
                strAmount = “”
            End If
            Count = Count + 1
        Loop
        Select Case BigDenom
            Case “”
                BigDenom = “No ” & strCur
            Case “One”
                BigDenom = “One ” & strCur
             Case Else
                BigDenom = BigDenom & ” ” & strCur
        End Select
        SpellNumber = BigDenom
    End If
End Function

‘ Converts a number from 100-999 into text
Function GetHundreds(ByVal MyNumber)
    Dim Result As String
    If Val(MyNumber) = 0 Then Exit Function
    MyNumber = Right(”000″ & MyNumber, 3)
    ‘ Convert the hundreds place.
    If Mid(MyNumber, 1, 1) <> “0″ Then
        Result = GetDigit(Mid(MyNumber, 1, 1)) & ” Hundred “
    End If
    ‘ Convert the tens and ones place.
    If Mid(MyNumber, 2, 1) <> “0″ Then
        Result = Result & GetTens(Mid(MyNumber, 2))
    Else
        Result = Result & GetDigit(Mid(MyNumber, 3))
    End If
    GetHundreds = Result
End Function

‘ Converts a number from 10 to 99 into text.
Function GetTens(TensText)
    Dim Result As String
    Result = “”           ‘ Null out the temporary function value.
    If Val(Left(TensText, 1)) = 1 Then   ‘ If value between 10-19…
        Select Case Val(TensText)
            Case 10: Result = “Ten”
            Case 11: Result = “Eleven”
            Case 12: Result = “Twelve”
            Case 13: Result = “Thirteen”
            Case 14: Result = “Fourteen”
            Case 15: Result = “Fifteen”
            Case 16: Result = “Sixteen”
            Case 17: Result = “Seventeen”
            Case 18: Result = “Eighteen”
            Case 19: Result = “Nineteen”
            Case Else
        End Select
    Else                                 ‘ If value between 20-99…
        Select Case Val(Left(TensText, 1))
            Case 2: Result = “Twenty “
            Case 3: Result = “Thirty “
            Case 4: Result = “Forty “
            Case 5: Result = “Fifty “
            Case 6: Result = “Sixty “
            Case 7: Result = “Seventy “
            Case 8: Result = “Eighty “
            Case 9: Result = “Ninety “
            Case Else
        End Select
        Result = Result & GetDigit _
            (Right(TensText, 1))  ‘ Retrieve ones place.
    End If
    GetTens = Result
End Function

‘ Converts a number from 1 to 9 into text.
Function GetDigit(Digit)
    Select Case Val(Digit)
        Case 1: GetDigit = “One”
        Case 2: GetDigit = “Two”
        Case 3: GetDigit = “Three”
        Case 4: GetDigit = “Four”
        Case 5: GetDigit = “Five”
        Case 6: GetDigit = “Six”
        Case 7: GetDigit = “Seven”
        Case 8: GetDigit = “Eight”
        Case 9: GetDigit = “Nine”
        Case Else: GetDigit = “”
    End Select
End Function

The formula : “=spellnumber(”1234.5678″,”Dinar”,”Fils”,3)” would therefore result in “One Thousand Two Hundred Thirty Four Dinar and Five Hundred Sixty Seven Fils”

<UPDATE> Bob Phillips at XLDynamic also has a different version here.

F9 formula in the formula bar

Filed under: General — Admin @ 7:37 am

A great tip from the Spy Journal : Using the F9 key in the formula bar in Excel

If you have a long formula in the formula bar and you want a quick way to see the results of part of the formula then you can use the F9 key. WARNING! Be careful how you use this because this will actually change the formula if you save the changes.

April 16, 2005

Does Tablet-PC rime with Excel ?

Filed under: Gadgets — Admin @ 10:12 am

If I still believed in Santa Claus, I think a Tablet PC would be on my list for this December. But, in my non-Elf and Excel-governed world, I think it’s wiser to carefully evaluate whether a Tablet PC is a wise investment . So after a bit of reading on the web (try “So you want to get a Tablet PC” for an introduction to “form factors”), I thought it might make sense to drop a note here and check if any of y’all Excelers uses a Tablet PC ? your thoughts ?

drop me a mail or post a comment.

April 15, 2005

XML - Introductory Article on MSDN

Filed under: Bookmarks, General, XML — Fadi @ 7:18 am

Interesting article on MSDN : Creating XML Mappings in Excel 2003

Walk through common XML tasks in Microsoft Office Excel 2003 to learn more about the new XML functionality in Office 2003 Editions. Learn how to add custom schemas, work with XML maps in Excel, and create a series of mappings based on various schemas. (19 printed pages)

April 14, 2005

Concatenate with Separators and Quotes with Exclude !

Filed under: User-Defined Functions — Fadi @ 8:04 am

A small revision to the ConcSep function where

  • the opening quote can be different from the closing quote
  • some values can be excluded from the quotes (used when the cell value is a function like to_date)

So here goes ConcSepQuote_Excl :

Public Function ConcSepQuote_Excl(InCells As Range, _
                Quot_St As String, Quot_End As String, _
                Sep As String, Excl As String) As String
    Dim OutStr As String
    Dim Cell As Variant
    
    OutStr = “”
    If InCells Is Nothing Then Exit Function
    On Error Resume Next
    For Each Cell In InCells
        If InStr(1, Cell.Value, Excl, vbTextCompare) > 0 Then
            OutStr = OutStr & Cell.Value & Sep
        Else
            OutStr = OutStr & Quot_St & Cell.Value & Quot_End & Sep
        End If
    Next Cell
    OutStr = Left(OutStr, Len(OutStr) - Len(Sep))
    ConcSepQuote_Excl = OutStr

and the revised example :

 ConcSepQuote_Excl

April 13, 2005

Dynamic Named Ranges

Filed under: Data Analysis — Admin @ 7:35 am

The more you work on Excel, the more indispensable Dynamic Ranges become. From simple data validation to conditional formatting and vlookups, dynamic ranges become the core referential element in Excel. The first page I ever read on the subject : Dynamic Pages on Ozgrid.com :

Possibly one of Excels most underutilised aspects is its ability to create dynamic named ranges that will expand and contract according to the data in them.

If you haven’t worked on them, now is the best time to start reading up.

April 12, 2005

Duplicates in a Column - Conditional Formatting

Filed under: Data Analysis, General — Fadi @ 8:04 am

On the newsgroups, I saw a question about detecting duplicate entries in a column. Jan Karel suggested the pre-emptive approach with data validation which will not allow the user to enter a duplicate entry.

There are cases where duplicate entries have to be allowed but a visual indication indicates the duplicate values. Conditional Formatting comes in handy in those cases :

Suppose B1:B20 holds the data. the conditional formatting should be applied by selecting the range and applying the “Formula Is” is : =COUNTIF($B$2:$B$20,B2)>1.

CondFormatDuplicate

Duplicate data can still be entered but is automatically indicated :

CondFormatResult

Welcome to ExcelPragma.com !

Filed under: Blogging — Admin @ 7:42 am

The set-up is getting cleaner by the day. Starting from today, this site will also answer calls for www.ExcelPragma.com. The RSS feed however is still unchanged.

I should (soon) move the feed over and fix my folders and permalinks. but for now, I’ll sit back and enjoy the new micro-accomplishment.

Concatenate with Separators and Quotes - revised

Filed under: User-Defined Functions — Fadi @ 7:33 am

The objective is to create a spreadsheet that could automatically generate an Oracle SQL script to “INSERT” data into a table. A typical INSERT statement looks like : INSERT INTO <table>(<field string names>) VALUES (<values string>);

The problem when trying to generate the strings from a range of cells is to

1- put the values in the cells between quotes.

2- exclude certain types of cells from being put between quotes (for example, date values).

3- concatenate the quoted and non-quoted cells into one string.

The ConcSep function was very useful in generating the final string but the function needed more tweaking to put the values between quotes (while allowing the user to specify the quote character/string). So here’s the function :

Public Function ConcSepQuote(InCells As Range, Quot As String, Sep As String) As String
    Dim OutStr As String
    Dim Cell As Variant
    
    OutStr = “”
    If InCells Is Nothing Then Exit Function
    On Error Resume Next
    For Each Cell In InCells
        OutStr = OutStr & Quot & Cell.Value & Quot & Sep
    Next Cell
    OutStr = Left(OutStr, Len(OutStr) - Len(Sep))
    ConcSepQuote = OutStr
End Function

and a small example :

 ConcSepQuote

« Older PostsNewer Posts »

Powered by WordPress