Results 1 to 5 of 5

Word Frequency Macro

Threaded View

  1. #1
    Registered User
    Join Date
    09-03-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    3

    Word Frequency Macro

    Hello,

    I know close to nothing about macros, but I needed one to find the frequency of words in a column. I found a macro that is approximately what I need, and it was working on my work computer (PC) but does not work on my Mac version of Excel.

    It is the Pivot Table section that is highlighted when I hit debug.

    This is the macro:


    Sub MakeWordList()
        Dim InputSheet As Worksheet
        Dim WordListSheet As Worksheet
        Dim PuncChars As Variant, x As Variant
        Dim i As Long, r As Long
        Dim txt As String
        Dim wordCnt As Long
        Dim AllWords As Range
        Dim PC As PivotCache
        Dim PT As PivotTable
        
        Application.ScreenUpdating = False
        Set InputSheet = ActiveSheet
        Set WordListSheet = Worksheets.Add(after:=Worksheets(Sheets.Count))
        WordListSheet.Range("A1") = "All Words"
        WordListSheet.Range("A1").Font.Bold = True
        InputSheet.Activate
        wordCnt = 2
        PuncChars = Array(".", ",", ";", ":", "'", "!", "#", _
            "$", "%", "&", "(", ")", " - ", "_", "--", "+", _
            "=", "~", "/", "\", "{", "}", "[", "]", """", "?", "*")
        r = 1
    
    '   Loop until blank cell is encountered
        Do While Cells(r, 1) <> ""
    '       covert to UPPERCASE
            txt = UCase(Cells(r, 1))
    '       Remove punctuation
            For i = 0 To UBound(PuncChars)
                txt = Replace(txt, PuncChars(i), "")
            Next i
    '       Remove excess spaces
            txt = WorksheetFunction.Trim(txt)
    '       Extract the words
            x = Split(txt)
            For i = 0 To UBound(x)
                WordListSheet.Cells(wordCnt, 1) = x(i)
                wordCnt = wordCnt + 1
            Next i
        r = r + 1
        Loop
        
    '   Create pivot table
        WordListSheet.Activate
        Set AllWords = Range("A1").CurrentRegion
        Set PC = ActiveWorkbook.PivotCaches.Add _
            (SourceType:=xlDatabase, _
            SourceData:=AllWords)
        Set PT = PC.CreatePivotTable _
            (TableDestination:=Range("C1"), _
            TableName:="PivotTable1")
        With PT
            .AddDataField .PivotFields("All Words")
            .PivotFields("All Words").Orientation = xlRowField
        End With
    End Sub

    Any idea as to what's going on?
    Last edited by Cutter; 09-03-2012 at 04:50 PM. Reason: Added code tags

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1