VBA basics
15. Four arithmetic operations and annotations (comments)

Excel macro basics and applications, introduction to Excel VBA
last updated:08-15-2019

15. Four arithmetic operations and annotations (comments)

Some of this topic was processed by machine translation. Original


Excel is a spreadsheet software, so if you can't calculate it, it won't be a story.


The first is the arithmetic operation (addition / subtraction remainder) operator.
This is the same as the worksheet operator.

Arithmetic operators



Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
\ Quotient of division
Mod remainder of division
^ Power

If used normally, there will be no problem.

If you need a calculation priority,
Please be sure to enclose with () and specify the calculation order.
Stop writing statements that rely on operator precedence as it is difficult to understand.
Therefore, we will not explain the priority order.

I do n’t think it ’s necessary, but as an example
A1 cell has a unit price of 300
In B1 cell, the quantity is 5
This is a case where 1500 is put in C1 cell as the amount of money.

Sub practice1()
  Cells(1, 3).Value = Cells(1, 1).Value * Cells(1, 2).Value
End Sub

This explanation is unnecessary.
The problem is that even if you look at this macro, it is not clear what calculation is being performed.
C1 cell = A1 cell * B1 cell
I understand this, but it's unclear what it means.

So, add a comment so that you know what you are doing.

Comment



Sub practice1()
  'Amount = Unit price * Quantity
  Cells(1, 3).Value = Cells(1, 1).Value * Cells(1, 2).Value
End Sub

In this way, a comment (annotation) follows' (single quotation).

Comments are not relevant to macro execution.
It has no effect on execution.

Comments can also be written from the middle of the line.

Sub practice1()
  Cells(1, 3).Value = Cells(1, 1).Value * Cells(1, 2).Value 'Amount = Unit price * Quantity
End Sub


'(Single quotation) is the only comment after,
Until then, you can write macros normally.

In this case, either of the above writing methods may be used.
I write comments for only one line behind
Comments on several lines are basically written from the beginning,
These are each person, and adapt to your organization's coding rules, if any.

In addition, in the case of a comment on a larger round (procedure, etc.)

'***************************************************************
'* Practice four arithmetic operations
'***************************************************************
Sub practice1()
  Cells(1, 3).Value = Cells(1, 1).Value * Cells(1, 2).Value 'Amount = Unit price * Quantity
End Sub


Like this, I write comments so that they stand out.
Since it is a comment, there is no particular rule on how to write it.
To make it easier to see when you see it later,
Write it so that it is easy to understand when you show it to others.



Articles with the same theme "VBA basics"

12. Variables and data types (Dim)
13. Constants and type declaration characters (Const)
14. Character concatenation and continuation line
15. Four arithmetic operations and annotations (comments)
16. Iterative processing (For Next)
17. Iterative processing (Do Loop)
18. Get last row (End, Rows.Count)
19. General practice question 1
20. Conditional branch (If)
21. Conditional branch (ElseIf)
22. Conditional branch (Select Case)



  • >
  • >
  • 15. Four arithmetic operations and annotations (comments)

  • If you find this site useful, please share and bookmark.


    I'm going to pay close attention to the description,
    If you have any mistakes or suggestions,「Contact Us」I hope you will let me know.
    Use the posted VBA code at your own risk.
    We are not liable for any damages such as data corruption.




    If you find this site useful, please share and bookmark.
    To the bottom of the text