web 2.0


How to: Excel 2007 Classic PivotTable AutoFormat

A few days ago, I was writing an article and someone asked me: how the heck do I use the classic AutoFormat for my Excel 2007 PivotTable?

The image below shows xlReport4 AutoFormat (the data is not in English, as the original article was not in English – but I am sure you get the picture):


Figure 1: Excel 2007 PivotTable Classic AutoFormat

Well, an alternative to this is to use VBA. The following code would suffice:

Sub Test()
    ActiveSheet.PivotTables("PivotTable1").Format xlReport6
End Sub

In this case, I use
xlReport6 instead of xlReport4. However, this approach may not be acceptable to all users, after all, not everyone is fluent with VBA. You could, of course, create a simple customization to apply this classic PivotTable format, but what about the other AutoFormat which are available?

Well, there is a simpler way to achieve that. Simply use the accelerator key combination. Follow these steps:

1. Select the Excel 2007 PivotTable
2. Type the key sequence: Alt à o à a. Notice that this sequence will vary according to the language. This sequence is for English. The sequence in Portuguese (BR), the language I was writing the original article, is Alt à f à a.

The Excel 2003 AutoFormat dialog box will open:


Figure 2: Excel 2003 Classic AutoFormat dialog box

Simply choose the AutoFormat you want and click OK. You’re set to go.

Tags: , , ,

Microsoft Excel | Microsoft Excel - VBA | PivotTable

How to: Windows Vista SMTP Server - Using CDO.Message

I had been using CDO object with Windows XP for a while and the code always ran without a problem, as long as the SMPT server was installed and properly set up. That was easy to do. As I moved the code to Windows Vista, I come to realize that my code no longer worked. I tried installing the SMTP Server on Windows Vista, but that was pointless since it is no longer available. I was having some trouble believing it was not there given that I used Windows Vista Ultimate, but fellow MVPs Bernard Cheah (http://www.iis.net/ http://msmvps.com/blogs/bernard/) and Gary VanderMolen confirmed my worries. Ron de Bruin, a fellow Excel MVP, also took part in the conversation about the issue. He also has an article on the subject which can be found here: http://www.rondebruin.nl/cdo.htm

According to Gary, this is pretty much useless anyway given that most ISPs nowadays block all port 25 traffic except to their own SMTP server.

In that case, I had to adapt things slightly so that I could route my emails through my SMTP Server. Nonetheless, I can still use Vista to route the email to a local pickup folder so that I can at least test things.

Here’s how it is done (uncomment the code to test locally):

Sub CDOTest_OnVista()
    Dim oCDO                    As Object
    Dim oCDOConfig              As Object
   
    Const c_sUsername           As String = "Your_Username_Goes_Here"
    Const c_sPassword           As String = "Your_Passoword_Goes_Here"
   
'   if you are using localhost to route the e-mail, you need to define
'   a pickup folder. In this case, I use: "C:\inetpub\mailroot\Pickup"
    Const c_sPickupFolder       As String = "C:\inetpub\mailroot\Pickup"
    Const c_iSMPTPort           As Integer = 25

   
    Dim iSendUsing              As Integer
    Dim sSMPTServerName         As String
   
   iSendUsing = 2      'remote host
'   iSendUsing = 1      'localhost

'   Define the SMTP Server here. "localhost" for using Vista's localhost
'   as the server. In the case below, you should use your own server.
'   sSMPTServerName = "localhost"
    sSMPTServerName = "Your_SMTP_server_goes_here" 'e.g.: smtp.youcompanyname.com
   
    Set oCDO = CreateObject("CDO.Message")
    Set oCDOConfig = CreateObject("CDO.Configuration")
   
    With oCDOConfig.fields
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = iSendUsing
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = sSMPTServerName
        .Item("http://schemas.microsoft.com/cdo/configuration/username") = c_sUsername
        .Item("http://schemas.microsoft.com/cdo/configuration/password") = c_sPassword
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = c_iSMPTPort
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory") = c_sPickupFolder
    End With

    With oCDO
        Set .Configuration = oCDOConfig
            .From = Sender@Email.com
            .To = Receiver@Email.com
            .Subject = "Test Message"
            .Configuration.fields.Update
            .Send
    End With
   
    Set oCDOConfig = Nothing
    Set oCDO = Nothing
   
End Sub

Tags: , ,

Microsoft Excel | Microsoft Excel - VBA | Vista

What is the factorial of 0.5 (0.5!)?

If you use Excel and enter the following formula: =FACT(0.5) the answer will be one. However, if you open a more rustic tool such as the Windows Calculator and try again, you will get 0.886226925510…

So, how can a tool such as the Windows calculator come up with the correct answer when Excel can only return 1? What about the factorial of 0.75 (0.75!)? Again, Excel returns 1 whereas the Windows calculator gets the correct answer: 0.919062526898...

Both numbers are close to one, but they are not exactly one.

The trouble is that Excel is using the general rule that states that the factorial is, in fact, the product of all integers in the series. So 4! Is given by =(4*3*2*1)=24. Unfortunately, the story does not end there for factorials.

When dealing with factorials the above works fine for integers. When dealing with non-integers (greater than zero), then we need the natural logarithm of the gamma function to get it to work. This function uses the concept of limits to determine the factorial of a number, so the factorial of 5 (5!) would be 119.99999998… when using gamma instead of the integer 120 (you can obviously round to 120, since it is effectively this number). For a discussion on the gamma function check this link: http://numbers.computation.free.fr/Constants/Miscellaneous/gammaFunction.html

Supposing the number you wish to find its factorial is located in cell A1, your formula using the gamma function would be:

=A1*EXP(GAMMALN(A1))

The last thing to rember is that this is for positive numbers only…

Tags: , , ,

Microsoft Excel

How to: Office 2007 CustomUI – getVisible attribute

When working with the Office 2007 CustomUI, you may want to toggle visibility of certain tabs or groups, for example. You accomplish this by using the getVisible attribute of the XML UI code and then wrapping it with a callback to handle the visibility.

In this article, I show you how to toggle the Office 2007 CustomUI visibility for a whole group and a group depending on the active worksheet (I will use Excel as the basis for this example). To begin with, we need to add the XML code to our XLSM file:

<customUI xmlns=
"http://schemas.microsoft.com/office/2006/01/customui" onLoad="rx_onLoad">
 <
ribbon
>
  <
tabs
>
   <
tab idMso="TabHome" label="My Home Tab" getVisible="rx_getVisibleTab"
>
    <
group idMso="GroupClipboard" getVisible="rx_getVisibleGroupClipboard"
/>
   </
tab
>
  </
tabs
>
 </
ribbon>

</customUI>

Notice that we have the following callbacks to generate:

·         rx_onLoad

·         rx_getVisibleTab

·         rx_getVisibleGroupClipboard


The first callback handles the setting of our ribbon object and the default values for the tab and group visibility. The second and third callbacks simply pick up the visibility boolean value and return it as the object’s visibility status (either the entire Home tab or the clipboard group or both).

Next, you should add a module where the following code should be added:

Option Explicit

Public rxMyRibbon                                       As IRibbonUI
Public rxblnTabHomeVisible                              As Boolean
Public rxblnGroupClipboardVisible                       As Boolean

Sub rx_onLoad(ribbon As IRibbonUI)
    Set rxMyRibbon = ribbon
    rxblnTabHomeVisible = True
    rxblnGroupClipboardVisible = True
End Sub

'Callback for TabHome getVisible
Sub rx_getVisibleTab(control As IRibbonControl, ByRef returnedVal)
    returnedVal = rxblnTabHomeVisible
End Sub

'Callback for GroupClipboard getVisible
Sub rx_getVisibleGroupClipboard(control As IRibbonControl, ByRef returnedVal)
    returnedVal = rxblnGroupClipboardVisible
End Sub


Finally, the visibility will depend on which sheet is active, thus in the code module for ThisWorkbook, you should add the following code:

Option Explicit
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Dim shIndex                    As Integer
   
    shIndex = Sh.Index
   
    Select Case shIndex
        Case 2
            rxblnTabHomeVisible = True
            rxblnGroupClipboardVisible = False
        Case 3
            rxblnTabHomeVisible = False
        Case Else
            rxblnTabHomeVisible = True
            rxblnGroupClipboardVisible = True
    End Select
   
    rxMyRibbon.Invalidate
End Sub


In the above scenario, if the active sheet has an index equal 2 (two); then the tab should be visible but not the clipboard group. When the index is 3 (three) then the whole tab should be invisible. Otherwise, all should be visible.

You can download the sample file here:
www.msofficegurus.com/uploads/getVisible.xlsm.

For further information on the Ribbon, you can this book:

Tags: , , ,

Microsoft Excel - VBA | Microsoft Office | Microsoft Office - VBA | Ribbon