Monday, October 13, 2014

VBA editor

-------
Insert > Macro > Module.
-------
To enable VBA:
File > Options > Customise the Ribbon > Developer box
-------
1. Right click anywhere on the ribbon, and then click Customize the Ribbon.
2. Under Customize the Ribbon, on the right side of the dialog box, select Main tabs (if necessary).
3. Check the Developer check box.
4. Click OK.
5. You can find the Developer tab next to the View tab.
-------
File > Options > Trust Center > Trust Center Settings > Macro settings > enable all macros.
-------
ツール >  マクロ > Visual Basic Editor
挿入 > 標準モジュール

プロジェクト ウィンドウ
プロパティ   ウィンドウ
コード         ウィンドウ

標準 モジュール
クラス モジュール
-------
Display messages to the Immediate window.

Debug.Print "message"
Debug.Print "variable1 = "; variable1
Debug.Print "The value of variable X is: " & X
Debug.Print X, Y, Z

Debug.Assert Var >= 0
This will pause on the Debug.Assert statement if Var >= 0 is False;

Type or paste a statement into the Immediate window, and then press ENTER.
print variable1
? variable1
? Backcolor
? Text1.Height
-------
To place a command button on your worksheet, execute the following steps.
1. On the Developer tab, click Insert.
2. In the ActiveX Controls group, click Command Button.
3. Drag a command button on your worksheet.
---------
To assign a macro (one or more code lines) to the command button, execute the following steps.
1. Right click CommandButton1 (make sure Design Mode is selected).
2. Click View Code.
3. Place your cursor between Private Sub CommandButton1_Click() and End Sub.
4. Add the code line shown below.
5. Close the Visual Basic Editor.
6. Click the command button on the sheet (make sure Design Mode is deselected).
--------
1. Open the Visual Basic Editor.
2. Double click on This Workbook in the Project Explorer.
3. Choose Workbook from the left drop-down list. Choose Open from the right drop-down list.
4. Add the following code line to the Workbook Open Event:
MsgBox "Good Morning"
5. Save, close and reopen the Excel file.
---------
1. Open the Visual Basic Editor.
2. Double click on a sheet (for example Sheet1) in the Project Explorer.
3. Choose Worksheet from the left drop-down list. Choose Change from the right drop-down list.
4. The Worksheet Change Event listens to all changes on Sheet1. We only want Excel VBA to do something if something changes in cell B2. To achieve this, add the following code lines:
If Target.Address = "$B$2" Then
End If
5. We only want Excel VBA to show a MsgBox if the user enters a value greater than 80. To achieve this, add the following code line between If and End If.
If Target.Value > 80 Then MsgBox "Goal Completed"
6. On Sheet1, enter a number greater than 80 into cell B2.

No comments:

Post a Comment