تمرین
SUB ZARB()
DIM I AS SINGLE
DIM J AS SINGLE
DIM K AS SINGLE
I= INPUTBOX(" ENTER I :")
J= INPUTBOX(" ENTER J :")
K=I*J
MSGBOX " I*J = " &K
END SUB
SUB ZARB()
DIM I AS SINGLE
DIM J AS SINGLE
DIM K AS SINGLE
I= INPUTBOX(" ENTER I :")
J= INPUTBOX(" ENTER J :")
K=I*J
MSGBOX " I*J = " &K
END SUB
و غیر عددی عا عبارتند از:
روش معرفی متغیر ها استفاده از کلمه ی کلیدی DIM بعد از نام ماکروست . به مثال زیر توجه کنید:
I به عنوان یک عدد صحیح ( متغیر) معرفی شده است .
SUB ALI()
DIM I AS INTEGER ' I به عنوان یک عدد صحیح معرفی
I = INPUTBOX (" ENTER A NUMBER INTEGER:")
END SUB
The following table shows the supported data types, including storage sizes and ranges.
Data type | Storage size | Range |
---|---|---|
Byte | 1 byte | 0 to 255 |
Boolean | 2 bytes | True or False |
Integer | 2 bytes | -32,768 to 32,767 |
Long (long integer) |
4 bytes | -2,147,483,648 to 2,147,483,647 |
Single (single-precision floating-point) |
4 bytes | -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values |
Double (double-precision floating-point) |
8 bytes | -1.79769313486231E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values |
Currency (scaled integer) |
8 bytes | -922,337,203,685,477.5808 to 922,337,203,685,477.5807 |
Decimal | 14 bytes | +/-79,228,162,514,264,337,593,543,950,335 with no decimal point; +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest non-zero number is +/-0.0000000000000000000000000001 |
Date | 8 bytes | January 1, 100 to December 31, 9999 |
Object | 4 bytes | Any Object reference |
String (variable-length) |
10 bytes + string length | 0 to approximately 2 billion |
String (fixed-length) |
Length of string | 1 to approximately 65,400 |
Variant (with numbers) |
16 bytes | Any numeric value up to the range of a Double |
Variant (with characters) |
22 bytes + string length | Same range as for variable-length String |
User-defined (using Type) |
Number required by elements | The range of each element is the same as the range of its data type. |
Note Arrays of any data type require 20 bytes of memory plus 4 bytes for each array dimension plus the number of bytes occupied by the data itself. The memory occupied by the data can be calculated by multiplying the number of data elements by the size of each element. For example, the data in a single-dimension array consisting of 4 Integer data elements of 2 bytes each occupies 8 bytes. The 8 bytes required for the data plus the 24 bytes of overhead brings the total memory requirement for the array to 32 bytes.
A Variant containing an array requires 12 bytes more than the array alone.
Note Use the StrConv function to convert one type of string data to another.
Sub ALI()
I = InputBox(" عدد مورد نظر را وارد کنيد ")
End Sub
در مثال فوق ماکرو ساده ای را مشاهده می کنید که پیام " عدد مورد نظر را وارد کنید " را نمایش داده و عدد یا متن کاربر را دریافت کرده و در متغییر I می ریزد .
شکل این تابع به صورت زیر است :
Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a String containing the contents of the text box.
Syntax
InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])
The InputBox function syntax has these named arguments:
Part | Description |
---|---|
prompt | Required. String expression displayed as the message in the dialog box. The maximum length of prompt is approximately 1024 characters, depending on the width of the characters used. If prompt consists of more than one line, you can separate the lines using a carriage return character (Chr(13)), a linefeed character (Chr(10)), or carriage return–linefeed character combination (Chr(13) & Chr(10)) between each line. |
title | Optional. String expression displayed in the title bar of the dialog box. If you omit title, the application name is placed in the title bar. |
default | Optional. String expression displayed in the text box as the default response if no other input is provided. If you omit default, the text box is displayed empty. |
xpos | Optional. Numeric expression that specifies, in twips, the horizontal distance of the left edge of the dialog box from the left edge of the screen. If xpos is omitted, the dialog box is horizontally centered. |
ypos | Optional. Numeric expression that specifies, in twips, the vertical distance of the upper edge of the dialog box from the top of the screen. If ypos is omitted, the dialog box is vertically positioned approximately one-third of the way down the screen. |
helpfile | Optional. String expression that identifies the Help file to use to provide context-sensitive Help for the dialog box. If helpfile is provided, context must also be provided. |
context | Optional. Numeric expression that is the Help context number assigned to the appropriate Help topic by the Help author. If context is provided, helpfile must also be provided. |
Remarks
When both helpfile and context are provided, the user can press F1 (Windows) or HELP (Macintosh) to view the Help topic corresponding to the context. Some host applications, for example, Microsoft Excel, also automatically add a Help button to the dialog box. If the user clicks OK or presses ENTER , the InputBox function returns whatever is in the text box. If the user clicks Cancel, the function returns a zero-length string ("").
Note To specify more than the first named argument, you must use InputBox in an expression. To omit some positional arguments, you must include the corresponding comma delimiter.
مثال :
Sub proInput()
J = InputBox("For what year do you need this report?")
End Sub
برای دانلود
ابتدا تمرین را دانلود کرده و سپس سعی کنید آنرا بدون ااستفاده از راهنما حل کنید:
صورت مسله : ماکرویی بنویسید که اعداد ستون اول را در در ستون دوم جستجو کرده و تکراری های آنرا یافته با رنگ سرخ و پررنگ نمایش دهد .
به طور مثال این ماکرو پیام " I AM ALI TOZIH " را نمایش می دهد .
Sub ALI4()
MsgBox " I AM ALI TOZIH "
End Sub
شکل این تابع به صورت زیر است :
MsgBox(prompt[, buttons] [, title] [, helpfile, context])
MSGBOX " متن جعبه ", buttons," عنوان جعبه"
ساختار این تابع به شرح زیر است :
Displays a message in a dialog box, waits for the user to click a button, and returns an Integer indicating which button the user clicked.
Syntax
MsgBox(prompt[, buttons] [, title] [, helpfile, context])
The MsgBox function syntax has these named arguments:
Part | Description |
---|---|
prompt | Required. String expression displayed as the message in the dialog box. The maximum length of prompt is approximately 1024 characters, depending on the width of the characters used. If prompt consists of more than one line, you can separate the lines using a carriage return character (Chr(13)), a linefeed character (Chr(10)), or carriage return – linefeed character combination (Chr(13) & Chr(10)) between each line. |
buttons | Optional. Numeric expression that is the sum of values specifying the number and type of buttons to display, the icon style to use, the identity of the default button, and the modality of the message box. If omitted, the default value for buttons is 0. |
title | Optional. String expression displayed in the title bar of the dialog box. If you omit title, the application name is placed in the title bar. |
helpfile | Optional. String expression that identifies the Help file to use to provide context-sensitive Help for the dialog box. If helpfile is provided, context must also be provided. |
context | Optional. Numeric expression that is the Help context number assigned to the appropriate Help topic by the Help author. If context is provided, helpfile must also be provided. |
Settings
The buttons argument settings are:
Constant | Value | Description |
---|---|---|
vbOKOnly | 0 | Display OK button only. |
vbOKCancel | 1 | Display OK and Cancel buttons. |
vbAbortRetryIgnore | 2 | Display Abort, Retry, and Ignore buttons. |
vbYesNoCancel | 3 | Display Yes, No, and Cancel buttons. |
vbYesNo | 4 | Display Yes and No buttons. |
vbRetryCancel | 5 | Display Retry and Cancel buttons. |
vbCritical | 16 | Display Critical Message icon. |
vbQuestion | 32 | Display Warning Query icon. |
vbExclamation | 48 | Display Warning Message icon. |
vbInformation | 64 | Display Information Message icon. |
vbDefaultButton1 | 0 | First button is default. |
vbDefaultButton2 | 256 | Second button is default. |
vbDefaultButton3 | 512 | Third button is default. |
vbDefaultButton4 | 768 | Fourth button is default. |
vbApplicationModal | 0 | Application modal; the user must respond to the message box before continuing work in the current application. |
vbSystemModal | 4096 | System modal; all applications are suspended until the user responds to the message box. |
vbMsgBoxHelpButton | 16384 | Adds Help button to the message box |
VbMsgBoxSetForeground | 65536 | Specifies the message box window as the foreground window |
vbMsgBoxRight | 524288 | Text is right aligned |
vbMsgBoxRtlReading | 1048576 | Specifies text should appear as right-to-left reading on Hebrew and Arabic systems |
The first group of values (0–5) describes the number and type of buttons displayed in the dialog box; the second group (16, 32, 48, 64) describes the icon style; the third group (0, 256, 512) determines which button is the default; and the fourth group (0, 4096) determines the modality of the message box. When adding numbers to create a final value for the buttons argument, use only one number from each group.
Note These constants are specified by Visual Basic for Applications. As a result, the names can be used anywhere in your code in place of the actual values.
Return Values
Constant | Value | Description |
---|---|---|
vbOK | 1 | OK |
vbCancel | 2 | Cancel |
vbAbort | 3 | Abort |
vbRetry | 4 | Retry |
vbIgnore | 5 | Ignore |
vbYes | 6 | Yes |
vbNo | 7 | No |
Remarks
When both helpfile and context are provided, the user can press F1 (Windows) or HELP (Macintosh) to view the Help topic corresponding to the context. Some host applications, for example, Microsoft Excel, also automatically add a Help button to the dialog box.
If the dialog box displays a Cancel button, pressing the ESC key has the same effect as clicking Cancel. If the dialog box contains a Help button, context-sensitive Help is provided for the dialog box. However, no value is returned until one of the other buttons is clicked.
Note To specify more than the first named argument, you must use MsgBox in an expression. To omit some positional arguments, you must include the corresponding comma delimiter.
مثال دیگر : در این مثال علاوه بر متن نمایش داده شده ، یک علامت تعجب زرد نیز دیده می شود و در ضمن کلمه "My Application" در بالای جعبه مشاهده می شود .
SUB ALI5()
MsgBox "Wrong button!@This button doesn't work.@Try Another.", vbOKOnly + Exclamation, "My Application"
END SUB
vbCritical | 16 | Display Critical Message icon. |
vbQuestion | 32 | Display Warning Query icon. |
vbExclamation | 48 | Display Warning Message icon. |
vbInformation |
Methodes
متد ها : عملیاتی هستند که برروی شیء انجام می گیرند . آنها نیز مانند خصوصیات شیء با یک نقطه به آن وصل می شوند. به طور مثال یکی از متد هایی که بروی شی worksheets فعال میشود ، select است . worksheets("sheet2").select این دستور باعث فعال شدن کاربرگ دوم می شود .
برخی از متد هایی اشیا در وی بی ای اکسل :
workbooks
Workbooks("book1").Close کارپوشه ی book1 را می بندد .
worksheets
Worksheets("sheet2").PrintPreview پیش نمایش چاپ کاربرگ را نشان می دهد.
Worksheets("sheet2").Protect "123" کاربرگ را با رمز 123 به حالت محافظت می برد .
Worksheets("sheet2").Unprotect "123" کاربرگ را با رمز 123 از حالت محافظت خارج می کند .
range
Range("a1").Clear محتویات و قالب بندی و توضیحات سلول a1 را حذف می کند .
Range("a1").ClearContents فقط محتویات سول را حذف می کند .
Range("a1").ClearFormats فقط قالب بندی سول را حذف می کند .
sub alisum()
range("b1") . formula = " = sum(a2:a1000) "
end sub
اشیا یی مانند range دارای تعدای خصوصیت به شرح زیر می باشند :
تمرین 1 - ماکرویی بنویسید که اندازه فونت محتویات سلول d3 را به 25 و حالت پررنگ تغییر دهد و نوع فونت را به titr .
sub ali23()
range("d3").font.name ="titr"
range("d3").font.size = 25
range("d3").font.bold = true
end sub
تمرین 1 - ماکروییی بنام EDITB1 بنویسید که محتویات سلول A1 را در درون سلول B1 قرار دهد .
SUB EDITB1()
RANGE("B1") = range("a1")
END SUB
تمرین 2- ماکرویی بنویسید که عدد 14 را درون c1 و عدد ۱۴- را درون c2 قرار دهد .
SUB ali22()
RANGE("c1") = 14
RANGE("c2") = -14
END SUB
بنام خدا
ماکرو نویسی در اکسل VBA
برای انجام عملیات تکراری و جستجو ی داده های مورد نظر می توان از ابزار قدرتمند ماکروها در اکسل استفاده کرد. ورود به ساختار برنامه نویسی ویژال بیسیک در اکسل که با عنوان VBA یا VISUAL BASIC FOR APPLICATION در اکسل و یا آفیس از آن یاد می گردد ، از مسیر زیر انجام می گیرد :
TOOLS | MACRO | VISAUL BASIC EDITOR
در حقیقت ماکرو ها، همان کدهایی هستد که توسط کاربر یا ماشین تولید می شوند. اگر شما یک ماکرو را با استفاده از ابزار ماکرو سازی طراحی و اجرا کنید آنرا توسط ماشین ساخته اید ولی اگر بخواهید انعطاف بیشتری به آن بدهید باید آنرا بنویسید( کد نویسی ). زبان وب بی ای در حقیقت معماری درونی اکسل و ابزاری قدرتمند برای نوشتن برنامه های پیشرفته و حلقوی است . این زبان که یک زبان شئ گراست ، اجزای درونی خود را به شکل اشیائی در نظر می گیرد که نرم افزار اکسل را تشکیل می دهند، مثل فایل ( کارپوشه WORKBOOK ) ، کاربرگ یا WORKSHEET و یا سلول RANGE .در حقیقت کارپوشه ها از اشیاء کاربرگ تشکیل شده اند و کاربرگ ها با اشیاء سلول کامل می شوند . هر شیء دارای یک سری خاصیت است . مثلا شما یک سنگ را در نظر بگیرید ، رنگ ، وزن ، شکل و... خواص سنگ محسوب می شوند .در وی بی ای VBA نیز همینطور است ، مثلا یک سلول دارای خواصی مثل محتویات ، اندازه فونت ، نام فونت ، رنگ ، فرمول ، کادر و .... می باشد. کاربران در برنامه نویسی در حقیقت این خواص را تغییر می دهند .
بیایید یک تمرین را باهم انجام دهیم :
ابتدا نوار ابزار ویژال بیسیک را فعال کنید ، سپس ماکرویی را طراحی کنید که در سلول A1 از کاربرگ 2 وارد شود .
مراحل ساخت ماکرو :
ابتدا کلید RECORD MACRO را می فشاریم و سپس به کاربرگ 2 و سلول A1 وارد می شویم و در آخر کلید STOP را می فشاریم .
حال برای دیدن کد ماکرو مسیر زیر را دنبال کنید :
کلید RUN را فشرده تا اسامی ماکرو ها ظاهر شود سپس ماکروی مورد نظر را انتخاب کرده کلید EDIT را می فشاریم تا متن ماکرو ( کد ) نمایش داده شود .
همانطور که مشاهده می کنید ماکرو با SUB و نام ماکرو ،شروع و با END SUB تمام می شود و در بین آنها عبارات برنامه نویسی نوشته شده است . سه رنگ در بدنه ی ماکرو بکار رفته 1- سبز: که معرف توضیحات برنامه است و هیچ تاثیری بر عملکرد ماکرو ندارد 2- آبی : کلمات کلیدی 3- سیاه : دستورات
Sub Macro1()
' Macro1 Macro
' Macro recorded 2007/04/23 by tozih
Sheets("Sheet2").Select
Range("A1").Select
End Sub
همانطور که از کد ها مشخص است ماکرویی بنام MACRO1 به کاربرگ 2 رفته و سلول A1 را در آن انتخاب می کند . در ساختار برنامه نویسی VBA تغییر خواص به صورت توارثی انجام می پذیرد ، یعنی هر خاصیت باید به شئ مورد نظر، با یک نقطه متصل باشد .
Range("A1").Select
در حقیقت برای تغییر خاصیت اشیاء باید ابتدا نام شئ را ذکر کرده سپس نقطه را قرار داده و آنگاه خاصیت مورد نظر را ذکر کنیم و مقادیر آنرا تغییر دهیم . مثلا :
RANGE("A2:A10").FONT.SIZE = 16
اندازه فونت محتویات سلول های A1 تا A10 را به 16 تغییر می دهد .
مثال دیگر ، محتویات سلول D2 از کاربرگ 3 را عبارت ALI TOZIH قرار دهید .
SHEETS("SHEET3").RANGE("D5").VALUE = "ALI TOZIH"
در ضمن خاصیت VALUE پیش فرض است و می توان آنرا حذف کرد :
SHEETS("SHEET3").RANGE("D5") = "ALI TOZIH"