Tip: Updates for Microsoft Office Excel 2007 Help

Hi all

Today I talked about the search results from the Excel 2007 help (Offline) with a fellow MVP. Our results where different, my results were correct and useful but his results on the same search topic were useless. The reason why we have different search results is because that I have installed the help update from 4/28/2009 that contains the latest revisions and additions to content in the Help files.

Note: Search “Offline” give the best search results.

How do you change it to Offline if it is set to “Connected to Office Online” ?

Press F1
If you see “Connected to Office Online” in the bottom right corner then click on this
and choose “Show content only from this computer

I suggest that you install this update to get better search results when you search offline.
You can get the updates through Microsoft Office update or download and install them by using the link below.
Maybe you have it already installed ?

Updates for Microsoft Office Excel 2007 Help
http://www.microsoft.com/downloads/details.aspx?FamilyID=2fbf47a2-e338-4ed8-92f2-b0a146e8593c&displaylang=en

Note: there are also updates for the other Office programs, see the links on the bottom of the page.

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm

Entering Dates Into Cells Part II

I'm sure you all remember Entering Dates Into Cells from five years ago. It was a simpler time. Brittney Spears was telling us about her prerogative, the Olympics returned to Athens, and my date entering needs reflected the simplicity of the times. Now I need more.

Here are some things I discovered about myself:

  • I almost never need to enter the time into a cell
  • I don't need to continue editing the cell after I press Cntl+; to enter the date
  • I often have to enter yesterday or tomorrow

Time to bloat the PMW again. I'm going to take over the Control+; (control plus semicolon) shortcut and the Control+Shift+; shortcut. The former will put the date into the activecell unless a date already exists there, in which case it will increment the date by one day. The latter will do the same except that it will decrement the date by one day.

You know what's great about a video demonstrating keyboard shortcuts? Anyway, here's what selecting C4, pressing Cntl+; three times, and pressing Cntl+Shift+; three times looks like:

In my Auto_Open and Auto_Close macros, respectively, I have these OnKey assignments

Application.OnKey "^;", "IncrementDate"
Application.OnKey "^+;", "DecrementDate"

Application.OnKey "^;"
Application.OnKey "^+;"

I should have made the following all one macro, but I'm a little lazy like that.

Sub IncrementDate()
   
    If IsDate(ActiveCell.Value) Then
        ActiveCell.Value = ActiveCell.Value + 1
    Else
        ActiveCell.Value = Date
        ActiveCell.NumberFormat = "m/d/yyyy"
    End If
   
End Sub
 
Sub DecrementDate()
   
    If IsDate(ActiveCell.Value) Then
        ActiveCell.Value = ActiveCell.Value - 1
    Else
        ActiveCell.Value = Date
        ActiveCell.NumberFormat = "m/d/yyyy"
    End If
   
End Sub

Reading XML Files in VBA

In Creating State Maps... I copied some data out of an XML file and graphed it into a state map. I wanted to try to read the XML file directly in VBA because I've never done it before. First, I set a reference to Microsoft XML v6. Six was the newest version I had, but I don't know what the differences are.

Next I started inspecting the objects via the Object Browser (F2). Definitely DOMDocument was the big one I would need and the Load method took a file name as an argument, so that looked promising. Here's what I ended up with:

Sub ReadStateXML()
   
    Dim xmlDom As MSXML2.DOMDocument
    Dim xmlPlaceMark As MSXML2.IXMLDOMNode
    Dim xmlPolygon As MSXML2.IXMLDOMNode
    Dim xmlCoord As MSXML2.IXMLDOMNode
    Dim sName As String
    Dim vaSpace As Variant, vaComma As Variant
    Dim i As Long, j As Long
   
    Set xmlDom = New MSXML2.DOMDocument
   
    xmlDom.Load "C:\Downloads\overlay_1198.kml"
   
    For i = 0 To xmlDom.childNodes(1).childNodes(0).childNodes.Length - 1
        If xmlDom.childNodes(1).childNodes(0).childNodes.Item(i).nodeName = "Placemark" Then
            Set xmlPlaceMark = xmlDom.childNodes(1).childNodes(0).childNodes.Item(i)
            Set xmlPolygon = xmlPlaceMark.childNodes(2).childNodes(0)
            Set xmlCoord = xmlPolygon.childNodes(0).childNodes(0).childNodes(0)
            sName = xmlPlaceMark.childNodes(1).childNodes(5).nodeTypedValue
               
            'Stop
           
            With Sheet4.Cells(Sheet2.Rows.Count, 1).End(xlUp).Offset(1, 0)
                .Value = sName
                vaSpace = Split(xmlCoord.childNodes(0).Text, " ")
                For j = LBound(vaSpace) To UBound(vaSpace)
                    vaComma = Split(vaSpace(j), ",")
                    .Offset(0, 1).Value = vaComma(0)
                    .Offset(0, 2).Value = vaComma(1)
                Next j
            End With
           
        End If
       
    Next i
   
End Sub

It almost gets the job done, but not quite. I use a lot of XMLNode variables so I can keep track of where I am, but the code is hardly self documenting. The childNodes property only takes an integer as an argument, so I can't call out the nodes name. This is only slightly better than reading it in as a text file. To be fair, I've only started messing with this object library, so maybe I can define a schema that makes things easier. I just don't know.

Based on this initial chart, it looks like a got the coordinates parsed correctly.

There are few dots over at x=0, y=50. Alaska has a few islands that cross the date line and have x coordinates around 179. Rather than try to convert them, I just converted the x's to zero. Ultimately I deleted those data points. Also, I should have made each polygon its own series. I don't know the limit on chart series, but I'll be I'd have reached it. Having only one series doesn't allow me to connect the dots.

I also added a trend line (in pink). It appears my country is in decline.

Euler Problem 123

Euler Problem 123 asks:

Let p(n) be the nth prime: 2, 3, 5, 7, 11, ..., and let r be the remainder when (p(n)-1)n + (p(n)+1)n is divided by p(n)2.

For example, when n = 3, p(3) = 5, and 43 + 63 = 280. 280 mod 25 = 5.

The least value of n for which the remainder first exceeds 109 is 7037.

Find the least value of n for which the remainder first exceeds 1010.

This is really the same problem as Euler 120 (posted last week) with p(n) taking the part of a. We are looking for 2*p(n)*n greater than 1010. I built a collection of the first 7037 primes, and then added primes one-by-one to the collection until the remainder was large enough, with the caveat that even n gives a remainder of 2, so we need an odd n.

All prime numbers beyond 3 lie left or right of an even multiple of six so a prime-checking routine need only check whether or not n mod 6 = 1 or 5 has even divisors, and even then, only up to the square root of n.

Here is the code that does that. The routine runs in about 2.5 seconds.

Sub Problem_123()
   Dim Answer As Long, T As Single
   Dim n As Long, i As Long, R As Double
   Dim p       As New Collection
 
   T = Timer
   
   p.Add Item:=2, Key:="2"   '1st Prime
   n = 1
   i = 3
 
   Do
      If IsPrime3(i) Then
         p.Add Item:=i, Key:=CStr(i)
         n = n + 1
      End If
      i = i + 2
   Loop Until n = 7037   'Primes in p
 
   Do
      If n Mod 2 = 0 Then
         R = 2
      Else
         R = 2 * p.Item(n) * n
      End If
      If R GT 10 ^ 10 Then
         Answer = n
         Exit Do
      End If
      i = p.Item(n) + 2
      Do Until IsPrime3(i)
         i = i + 2
      Loop
      n = n + 1
      p.Add Item:=i, Key:=CStr(i)
   Loop
 
   Debug.Print Answer; "  Time:"; Timer - T, p.Item(Answer), R
End Sub
 
Function IsPrime3(Num As Variant) As Boolean
   Dim i       As Long
   
   If Num  != Int(Num) Then
      Exit Function                                      'IsPrime = False
   Else
      Num = CDec(Num)
   End If
   If Num LT 2 Then Exit Function                         'IsPrime = False
   If Num = 2 Then
      IsPrime3 = True
      Exit Function
   End If
   If Num = 3 Then
      IsPrime3 = True
      Exit Function
   End If
   
   Select Case Num Mod 6
      Case 1, 5
         For i = 3 To Sqr(Num) Step 2
            If Num Mod i = 0 Then Exit Function          'IsPrime = False
         Next i
      Case Else
         Exit Function                                   'IsPrime = False
   End Select
   
   IsPrime3 = True
End Function

Dick has more on prime numbers here. The usual angle bracket corrections are in the above.

...mrt

Hide or display Custom Ribbon Tabs, Groups or Controls with VBA code

Hi all

Today I upload a new page with examples to Hide or display Custom Tabs, Groups or Controls with VBA code

Hide or Display Custom Ribbon Tab/Group/Control with getVisible
http://www.rondebruin.nl/hidevisible.htm

If you have suggestions to make the examples better please let me know.
I am no expert but love to try new stuff like this.

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm

Creating State Maps with XY Charts

According to eggheadcafe, Excel doesn't have built-in maps anymore. I'm not a charting guy, as you know, so I didn't know that. I also didn't know that they ever did. But I wanted a map of the US with sales by region. That seems possible, but all I got this time was a map of Nebraska. Kind of a proof of concept before I'm ready to tackle the whole US. Here's how I got it:

First I went to Finder to get a KML file. A KML file appears to be a special purpose XML file. You know XML? That data format that's perfect for everything in every situation? When I got to Finder, I noted a CSV download. Perfect, I thought. Alas, the CSV does not provide the same information as the KML file, so I had to go the long way. I'm not sure what good that CSV file is. That is, I don't really know what you could make out of that data.

I downloaded the KML file and started inspecting it. There is a coordinates tag that plots the outline of every state. Some states, like Nebraska, have one coordinates tag because we are a closed polygon of a state. States that touch water, however, have more than one coordinates tag because there's always an island. Alaska has ten million coordinates tags (not really, but it's a lot).

kml nebraska coordinates

If I had Office 2003 Professional, I could probably get that XML file right into a spreadsheet. But I don't. I have Small Business Edition - no XML facilities in here. Although I intended (and intend) to make a map of the US, I thought I'd start simply and just do one state - my state. I copied the coordinates tag with all of those coordinates and pasted into a text file. Then I used this macro to list them in a worksheet.

Sub GetCoordinates()
   
    Dim sFname As String
    Dim lFnum As Long
    Dim sLine As String
    Dim vaCoords As Variant
    Dim vaIndiv As Variant
    Dim i As Long
   
    lFnum = 1
    sFname = Environ$("userprofile") & "\My Documents\NE_Coordinates.txt"
   
    Open sFname For Input As lFnum
   
    Do While Not EOF(lFnum)
        Line Input #lFnum, sLine
        vaCoords = Split(sLine, " ")
        For i = LBound(vaCoords) To UBound(vaCoords)
            vaIndiv = Split(vaCoords(i), ",")
            Sheet3.Cells(Sheet3.Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(1, 2).Value = vaIndiv
        Next i
    Loop
   
    Close lFnum
   
End Sub

I actually tried pasting the coordinates from the XML (opened in Firefox) directly into Excel and splitting them out with Text to Columns and some transposing, but I think I ran into a character limit - not everything pasted. So I went with the text file/VBA method.

With the data in Excel, I created an XY Scatter Chart. I removed the axis, gridlines, makers, and connected the data points with lines.

excel chart us states nebraska

Not too bad. I'm not sure how to ensure the axes are on the same scale. This looks a little horizontally squashed. Here's what it looks like before the clean up.

excel chart us states nebraska

I need to read the XML file directly into VBA. There are some XML libraries (under VBE - Tools - Reference) that look promising, but I haven't tried them yet. Then I should be able to draw the whole country without a lot of effort.

Bacon Bits Blog

Welcome to 2002, Mike! In another five years you'll be on Facebook and Twitter. I kid because I love.

Mike Alexander of DataPig Technologies has started Bacon Bits, "Delicious pieces of Excel and Access training". It promises to be the feel-good Excel blog of the year. Check it out.

Subscribed!

Work Breakdown Structure Numbering in Excel

I've never heard of Work Breakdown Structure (WBS) before, but I've definitely seen it. It's that type of numbering I see in lawerly type documents. It looks like this:

Jeremy has seen it before and wrote an Excel macro to number an Excel list based on indentation.


It seemed to work well for me. Well, I didn't read the instructions first which put me in an infinite loop. But once I broke out of that, read the instructions, and reformatted my data, it worked. In that regard, I'd add a If r > Rows.Count Then Exit Do in there just for good measure. Of course, I would use class modules to do this, but then Mike Alexander would just make fun of me for it. :)