VBA example - Excel 2007: VBA loop to add comments

VBA loop to add comments

Suppose that your Excel sheet contains the information about daily sales of products as shown below. Now let say you want to add comment to the sales that are the best. For example, the best sales today are the sales that have Qty(quantity) greater than 100 units.

ProductName Price Qty
Coca Cola 89 4
Angkor beer 34 205
Pepsi 23 34
Palm beer 45 32
ABC beer 34 54
Randonal  53 105
Sachdom 54 120

To run this example VBA code, you will need a Form, and a CommandButton. You may copy or type the sample table of sales above in to your current worksheet.

VBA MS Excel: VBA loop to add comments to target rows

VBA code for this example:

Option Explicit
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 2 To 8
If Range("C" & i) > 100 Then
Range("C" & i).AddComment "Good sales of the day!"
End If
End Sub



Enter the text:

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.