Japanese

VBA macro and Immediate

Excel and Word applications equip versatile utility i.e. Visual Basic Application(VBA) which is talked about to have a high obstacle for practical use. The fatal drawback of VBA may be almost defenseless toward malice-bearer. It is very easy to use programs developed by other people, however, there are so many risks including unwilling result, ending, and freezing even if malicious project is exempted.

How to practice Macro

The simple way to practice macro program.

1. On action of [Alt + F11] performance in both Excel and Word application, Visual basic window opens as shown in the Figure.

2. [insert] → [module] (

3. Paste a reliable "Procedure (program)" [Sub  …  〜 End Sub] ()

4. If you believe the program reliable click [practice macro] (). If you would like to process stepwise, push F8 key for each line.

Practical Use of Immediate Window

If you type following code in an Immediate Window and push [Enter] key, the figure "15" appears in a [Message Box].

x=3: y=5: Msgbox x*y

In the case of following, the [a1 〜 c4] cell-range of an active-cell may be selected. If the active cell is B4, the range of [ b4 〜 d7 ] may be high-lighted.

ActiveCell.Range("a1:c4").Select

In Word application the following description selects the first sentence of the second paragraph in an active document.

ActiveDocument.Paragraphs(2).Range.Sentences(1).Select

 As is mentioned above, [Msgbox] and [Select] are very convenient manner for accepting an information on intended region.

Application…multiplication table

 For i=1 To 9:For j=1 To 9:ActiveCell.Cells(i,j)=i*j:Next:Next

The 9 X 9 range of an active cell will be filled with multiplied results.

Example…Deletion and Insertion of every second line

Set c=[a4]:For i=6 To 1000 Step 2:Set c=Union(c,Cells(i,1)):Next:c.EntireRow.Delete

The above description deletes all lines of even number ranging between 4 to 1000 lines.


For i=4 To 100 Step 2:Cells(i,1).EntireRow.Insert:Next

On the other hand, the above inserts every second line in the range of 4 to 100 lines.

このサイトで扱った項目