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.
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.
If you type following code in an Immediate Window and push [Enter] key, the figure "15" appears in a [Message Box]. 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. In Word application the following description selects the first sentence of the second paragraph in an active document.
As is mentioned above, [Msgbox] and [Select] are very convenient manner for accepting an information on intended region. The 9 X 9 range of an active cell will be filled with multiplied results.
The above description deletes all lines of even number ranging between 4 to 1000 lines.
On the other hand, the above inserts every second line in the range of 4 to 100 lines.
Practical Use of Immediate Window
x=3: y=5: Msgbox x*y
ActiveCell.Range("a1:c4").Select
ActiveDocument.Paragraphs(2).Range.Sentences(1).Select
Application…multiplication table
For i=1 To 9:For j=1 To 9:ActiveCell.Cells(i,j)=i*j:Next:Next
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
For i=4 To 100 Step 2:Cells(i,1).EntireRow.Insert:Next
このサイトで扱った項目