Complete Code:
Sub MakeFreqTable()
Dim CloudData As Range
Dim Pt As PivotTable
Dim strField As String
Dim oDic As Object
Dim varData
Dim varItems
Dim varKeys
Dim n As Long
Dim wksTable As Worksheet
Dim lngTop5Count As Long
Const cstrSHEET_NAME As String = "Incident Summary"
On Error Resume Next
'Name the highlighted column range "CloudData"
Set CloudData = Selection
On Error GoTo err_handle
Application.ScreenUpdating = False
If Not CloudData Is Nothing Then
Set oDic = CreateObject("Scripting.Dictionary")
strField = Cells(1, CloudData.Column).Value
With CloudData
If .Row = 1 Then
varData = .Resize(.Rows.Count - 1).Offset(1).Value
Else
varData = .Value
End If
End With
For n = 1 To UBound(varData, 1)
If Len(varData(n, 1)) > 0 Then
oDic(CStr(varData(n, 1))) = Val(oDic(CStr(varData(n, 1)))) + 1
End If
Next n
If oDic.Count > 0 Then
On Error Resume Next
Application.DisplayAlerts = False
Sheets(cstrSHEET_NAME).Delete
Application.DisplayAlerts = True
On Error GoTo err_handle
Set wksTable = Sheets.Add
With wksTable
.Name = cstrSHEET_NAME
.Range("B2:C2").Value = Array(strField, "Total")
varItems = oDic.Items
varKeys = oDic.Keys
If oDic.Count > 5 Then
lngTop5Count = Application.Large(varItems, 5)
Else
lngTop5Count = 0
End If
For n = LBound(varItems) To UBound(varItems)
If varItems(n) >= lngTop5Count Then
With .Cells(.Rows.Count, "B").End(xlUp).Offset(1)
.Value = varKeys(n)
.Offset(, 1).Value = varItems(n)
End With
End If
Next n
'Sorts frequency table in descending order.
With .Range("B2").CurrentRegion
.Sort .Cells(1, 2), xlDescending
End With
End With
End If
End If
With ActiveSheet.OLEObjects.Add(ClassType:="Shell.Explorer.2", Link:=False, _
DisplayAsIcon:=False, Left:=200, Top:=15, Width:=325, Height:=225).ShapeRange
.ScaleWidth 1.480369515, msoFalse, msoScaleFromTopLeft
.ScaleHeight 1.3966666667, msoFalse, msoScaleFromTopLeft
End With
leave:
Application.ScreenUpdating = True
Call test(CloudData)
Exit Sub
err_handle:
MsgBox Err.Description
Resume leave
End Sub
Public Sub test(rng As Range)
'this subroutine produces a tag cloud and places it within the Web Browser contained
'on "Incident Summary" (cstrSHEET_NAME) worksheet. It does this by calling WordCloud
'subroutine which creates the tag cloud using a jscript file stored locally.
WordCloud rng
End Sub
Sub WordCloud(rngInput As Range)
Dim wbString As String
Dim myFile As String
Dim rngVar As Variant
Dim fnum As Integer
Dim i As Integer
Dim LastRow As Integer
rngVar = Application.Transpose(rngInput.Value)
wbString = "<html>" & vbCr
wbString = wbString & " <head>"
wbString = wbString & " <link rel=""stylesheet"" type=""text/css"" href=""wc.css""></script>" & vbCr
wbString = wbString & " <script type=""text/javascript"" src=""wcbackup3.js""></script>" & vbCr
wbString = wbString & " <script type=""text/javascript"" src=""jsapi""></script>" & vbCr
wbString = wbString & " </head>" & vbCr
wbString = wbString & " <body>" & vbCr
wbString = wbString & " <div id=""wcdiv""></div>" & vbCr
wbString = wbString & " <script type=""text/javascript"">" & vbCr
wbString = wbString & " google.load('visualization', '1');" & vbCr
wbString = wbString & " google.setOnLoadCallback(draw);" & vbCr
wbString = wbString & " function draw() {" & vbCr
wbString = wbString & " var data = new google.visualization.DataTable();" & vbCr
wbString = wbString & " data.addColumn('string', 'Text1');" & vbCr
wbString = wbString & " data.addRows(" & UBound(rngVar) & ");" & vbCr
For i = 1 To UBound(rngVar)
wbString = wbString & " data.setCell(" & i - 1 & ", 0,'" & rngVar(i) & "');" & vbCr
Next i
wbString = wbString & " var outputDiv = document.getElementById('wcdiv');" & vbCr
wbString = wbString & " var wc = new WordCloud(outputDiv);" & vbCr
wbString = wbString & " wc.draw(data, null);" & vbCr
wbString = wbString & " }" & vbCr
wbString = wbString & " </script>" & vbCr
wbString = wbString & " </body>" & vbCr
wbString = wbString & "</html>"
myFile = ThisWorkbook.Path & "\WordCloud.htm"
fnum = FreeFile()
Open myFile For Output As fnum
Print #fnum, wbString
Close #fnum
With Application
.ScreenUpdating = False
.DisplayAlerts = False
With Sheets.Add
.Delete
End With
.ScreenUpdating = True
.DisplayAlerts = True
End With
With Sheets("Incident Summary").OLEObjects("WebBrowser1").Object
'With Sheets("Incident Summary").WebBrowser1
.Silent = True
.Navigate (myFile)
' Do
' DoEvents
' Loop Until .ReadyState = READYSTATE_COMPLETE
Do
DoEvents
Loop While .Busy
' .Document.body.Scroll = "no"
End With
'MsgBox "Macro Finished."
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayHeadings = False
Range("B2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
'ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$B$5"), , xlYes).Name = "SummaryTable"
ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes).Name = "SummaryTable"
Range("SummaryTable[#All]").Select
ActiveSheet.ListObjects("SummaryTable").TableStyle = "TableStyleMedium17"
Selection.FormatConditions.AddColorScale ColorScaleType:=3
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).ColorScaleCriteria(1).Type = xlConditionValueLowestValue
With Selection.FormatConditions(1).ColorScaleCriteria(1).FormatColor
.Color = 8109667
.TintAndShade = 0
End With
Selection.FormatConditions(1).ColorScaleCriteria(2).Type = xlConditionValuePercentile
Selection.FormatConditions(1).ColorScaleCriteria(2).Value = 50
With Selection.FormatConditions(1).ColorScaleCriteria(2).FormatColor
.Color = 8711167
.TintAndShade = 0
End With
Selection.FormatConditions(1).ColorScaleCriteria(3).Type = xlConditionValueHighestValue
With Selection.FormatConditions(1).ColorScaleCriteria(3).FormatColor
.Color = 7039480
.TintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Rows("1:1").RowHeight = 100
Columns("A:A").ColumnWidth = 25
End Sub
The bit the code needed:
With Application
.ScreenUpdating = False
.DisplayAlerts = False
With Sheets.Add
.Delete
End With
.ScreenUpdating = True
.DisplayAlerts = True
End With
Still have a few tweaks yet to go...one being that it shows more than the top 5 in the frequency table, i'd rather it only summarize the top 5
Bookmarks