Category: Excel

  • Word to Excel Breaks

    1. In Word replace all the paragraph marks with a unique character.
    2. In Excel replace the unique character with the Excel line break character.
    3. In the Replace field, enter the following Alt code: Alt+0010. This code enters in a single line break. You will not see this character but the cursor may change.

    Note, to enter an Alt code, hold down the Alt key as you type the digits on the numeric keypad. It may help to have Num Lock on.

    Reference

  • Excel Reverse Pivot Table

    Excel VBA to reverse a Pivot Table. You can always trust finding Excel stuff if you use J-Walk as part of the search.

    Sub ReversePivotTable()
      ' Before running this, make sure you have a summary table with column headers.
      ' The output table will have three columns.
      Dim SummaryTable As Range, OutputRange As Range
      Dim OutRow As Long
      Dim r As Long, c As Long
      On Error Resume Next
      Set SummaryTable = ActiveCell.CurrentRegion
      If SummaryTable.Count = 1 Or SummaryTable.Rows.Count < 3 Then
        MsgBox "Select a cell within the summary table.", vbCritical
        Exit Sub
      End If
      SummaryTable.Select
      Set OutputRange = Application.InputBox(prompt:="Select a cell for the 3-column output", Type:=8)
      ' Convert the range
      OutRow = 2
      Application.ScreenUpdating = False
      OutputRange.Range("A1:C3") = Array("Column1", "Column2", "Column3")
      For r = 2 To SummaryTable.Rows.Count
        For c = 2 To SummaryTable.Columns.Count
          OutputRange.Cells(OutRow, 1) = SummaryTable.Cells(r, 1)
          OutputRange.Cells(OutRow, 2) = SummaryTable.Cells(1, c)
          OutputRange.Cells(OutRow, 3) = SummaryTable.Cells(r, c)
          OutputRange.Cells(OutRow, 3).NumberFormat = SummaryTable.Cells(r, c).NumberFormat
          OutRow = OutRow + 1
        Next c
      Next r
    End Sub
    
  • Excel Power Utility Pak

    I first found out about John Walkenback from his books. He, without question, has the best books on Excel. Back when books only were made from dead trees I judged books by how much real information versus fluff. He wins outright. His books now are available in paper format or electronically.

    He also has a site with many good Excel tips.

    For many years I have also used his add-in. It is another reason why I am running Excel on Windows as the Mac does not support these add-ins.

    In fact even if it had only the cells items

    And the text tools items

    It is well worth the money!

    I also get the source code which he sells for an additional price. That opens up many opportunities for programming or learning programming.

    On his site, and I agree he says:

    PUP is an Excel add-in that adds dozens of new features and functions to Excel. A free 30-day trial version is available.

    There is also an Enhanced Data Form for free.

    The J-Walk Enhanced Data Form is a free Excel add-in that provides a general-purpose data entry dialog box. The VBA code is available for a small fee.

    There are other competing Excel add-ins. But I trust J-Walk’s and it is just the right amount of add-ins in the menu bar.

    If you ever have questions or issues with Pup, John replies very quickly to any support request.

    I had a glitch installing the latest version of Pup, enabling the worksheet functions, and found out that it was my Excel file.

    • You need to ensure that a workbook’s VB project is not protected. If you unprotect your VB project, it should work.
    • And, you also need to make sure the workbook is not shared.
  • Hyphen Minus Dash

    I was working in Excel matching some strings and they were not matching even though they looked the same on the screen.
    Well it turns out that there are different kinds of dashes!
    A normal dash you use is the keyboard is the minus sign on your keyboard. But there are others that can get put in. In my case there were some normal minus characters but there were also some actual dash characters.
    The difference as described in the link above is the familiar “-”

    Ascii hyphen, with multiple usage, or “ambiguous semantic value”; the width should be “average”.

    But somehow in the data there was also this character, “?” which is

    unambiguously a hyphen character, as in “left-to-right”; narrow width.

    It took some digging to figure this out, looking at the boolean values in the text to see what characters were there.
    So in order to spare me future dash grief, I put together the following Excel function. It’s self explanatory.
    Note that it also deletes spaces as I don’t want any leading or trailing ones in the strings I’m dealing with in Excel. You could comment those lines out of course.

    Function tagfix(ss As String) As String
    Dim Counter As Integer
    Dim s As String
    tagfix = ""
    For Counter = 1 To Len(ss)
    s = Mid(ss, Counter, 1)
    Select Case s
    ' Comment out the following line
    ' if you don't want spaces removed
    Case " "
    ' skip spaces
    ' do nothing
    Case 0 To 9
    tagfix = tagfix & s
    Case "a" To "z"
    tagfix = tagfix & UCase(s)
    Case "A" To "Z"
    tagfix = tagfix & s
    Case Else
    tagfix = tagfix & "-"
    End Select
    Next
    

    Wait there’s more! If you setup the following subroutine you can run the fix on a selection.

    Dim c As Range
        For Each c In Selection
            c = tagfix(c.Value)
            Next c
    End Sub
    
  • Excel Parser

    I had written my own but it had a bug. Then I found this code below on this site.
    It gives credit to John Walkenbach who writes, bar none, the best books on Excel.

    Public Function ExtractElement(Txt, n, Separator) As String
    ' Returns the nth element of a text string, where the
    ' elements are separated by a specified separator character
    Dim Txt1 As String, temperament As String
    Dim ElementCount As Integer, i As Integer
    Txt1 = Txt
    ' If space separator, remove excess spaces
    If Separator = Chr(32) Then Txt1 = Application.Trim(Txt1)
    ' Add a separator to the end of the string
    If Right(Txt1, Len(Txt1)) <> Separator Then _
    Txt1 = Txt1 & Separator
    ' Initialize
    ElementCount = 0
    TempElement = ""
    ' Extract each element
    For i = 1 To Len(Txt1)
    If Mid(Txt1, i, 1) = Separator Then
    ElementCount = ElementCount + 1
    If ElementCount = n Then
    ' Found it, so exit
    ExtractElement = TempElement
    Exit Function
    Else
    TempElement = ""
    End If
    Else
    TempElement = TempElement & Mid(Txt1, i, 1)
    End If
    Next i
    ExtractElement = ""
    End Function
    


    And the following function returns nothing more than the number of elements you get when you cut a string according to a particular delimiter.

    Public Function ExtractElementCount(Txt, Separator) As String
    ' Returns the number of different elements in a string
    ' separated by a specified separator character
    ' This is useful if you are, for example trying to grab the last element or the second to last.
    Dim Txt1 As String, temperament As String
    Dim ElementCount As Integer, i As Integer
    Txt1 = Txt
    ' If space separator, remove excess spaces
    If Separator = Chr(32) Then Txt1 = Application.Trim(Txt1)
    ' Add a separator to the end of the string
    If Right(Txt1, Len(Txt1)) <> Separator Then _
    Txt1 = Txt1 & Separator
    ' Initialize
    ElementCount = 0
    TempElement = ""
    ' Extract each element
    For i = 1 To Len(Txt1)
    If Mid(Txt1, i, 1) = Separator Then
    ElementCount = ElementCount + 1
    End If
    Next i
    ExtractElementCount = ElementCount
    End Function
    

    Footnote

    I see that John Walkenbach has a post about The Versatile Split Function

    VBA’s Split function, introduced with Excel 2000, can simplify many programming tasks. This function accepts a text string, and returns a zero-based variant array that contains the elements of the string (you specify the character that delimits the elements).

    And then you can replace the ExtractElement with just this! Wow!

    Function ExtractElement(str As String, n As Integer, sepChar As String) As Variant
    ' Returns the nth element from a string,
    ' using a specified separator character
    Dim x As Variant
    x = Split(str, sepChar)
    If n > 0 And n - 1 < = UBound(x) Then
    ExtractElement = x(n - 1)
    Else
    ExtractElement = ""
    End If
    End Function
    


    And in case you haven’t had enough you can just do a word count with this.

    Function WordCount(txt as String) As Long
    ' Returns the number of words in a string
    Dim x As Variant
    txt = Application.Trim(txt)
    x = Split(txt, " ")
    WordCount = UBound(x) + 1
    End Function
    


    There are some more examples on the j-walk site.

    • extract a path or a filename from a full filespec
    • Counting specific characters in a string
    • Finding the longest word