Home>Blogs>Charts and Visualization>Word Cloud in Excel Using VBA
Word Cloud
Charts and Visualization

Word Cloud in Excel Using VBA

Word Cloud is also known as text cloud or tag cloud. Size of the words change according to the value or weightage or frequency of any word. Here we have created a macro to create the word cloud in Excel. In this macro file, we have 2 worksheets

Setting Worksheet – In this worksheet, you need to enter your data, like we have entered the Country wise population. We have given small setting section wherein you can enter the Minimum Font Size, Maximum Font Size, Separator, Multiple Font Color (Yes or No) and Multiple Font Name (Yes or No)

Setting Sheet
Setting Sheet

Word Cloud Worksheet – Once you will click on “Create Word Cloud” Button. It will create a word cloud on this worksheet.

Word Cloud
Word Cloud

 

Below is the code to create a word cloud-

Option Explicit

Sub Create_Word_Cloud()

Dim Setting_Sh As Worksheet
Set Setting_Sh = ThisWorkbook.Sheets("Setting")

Dim WCSh As Worksheet
Set WCSh = ThisWorkbook.Sheets("Word Cloud")


Dim Min_Font_Size As Integer
Dim Max_Font_Size As Integer

Min_Font_Size = Setting_Sh.Range("H6").Value
Max_Font_Size = Setting_Sh.Range("H7").Value


Dim Max_Value As Long
Max_Value = Application.WorksheetFunction.Max(Setting_Sh.Range("B:B"))

Dim i As Integer

Dim start_ As Integer
Dim length_ As Integer


WCSh.Cells.Delete
WCSh.Range("B2").Locked = False

For i = 3 To Application.CountA(Setting_Sh.Range("A:A"))

If i = 3 Then
WCSh.Range("B2").Value = Setting_Sh.Range("A" & i).Value
Else
WCSh.Range("B2").Value = WCSh.Range("B2").Value & Setting_Sh.Range("H8").Value & Setting_Sh.Range("A" & i).Value
End If

Next i

For i = 3 To Application.CountA(Setting_Sh.Range("A:A"))

length_ = Len(Setting_Sh.Range("A" & i).Value)
start_ = Application.WorksheetFunction.Search(Setting_Sh.Range("A" & i).Value, WCSh.Range("B2").Value)

With WCSh.Range("B2").Characters(start_, length_).Font

.ColorIndex = IIf(Setting_Sh.Range("H9").Value = "Yes", Application.WorksheetFunction.RandBetween(3, 56), vbBlack)

.Bold = True

.Size = Min_Font_Size + Setting_Sh.Range("B" & i).Value * ((Max_Font_Size - Min_Font_Size) / Max_Value)

If Setting_Sh.Range("H10").Value = "Yes" Then
.Name = Setting_Sh.Range("P" & Application.WorksheetFunction.RandBetween(2, Application.CountA(Setting_Sh.Range("P:P"))))
End If
End With
Next i

With WCSh.Range("B2")
.EntireRow.AutoFit
.WrapText = True
.EntireColumn.ColumnWidth = 130
.HorizontalAlignment = xlCenter
.Borders.LineStyle = xlDouble
End With

WCSh.Activate
WCSh.Range("A1").Select
ActiveWindow.DisplayGridlines = False

End Sub

Click here to download the Practice file-

 

Watch the step by step video tutorial:

PK
Meet PK, the founder of PK-AnExcelExpert.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your Excel skills to the next level!
https://www.pk-anexcelexpert.com