当前位置:网站首页>VB variable types and control statements (basic)
VB variable types and control statements (basic)
2022-07-25 06:07:00 【N-order magic cube】
Section 1 : Data types and common control statements
1、 Operator :
(1) Assignment operator =
(2) Mathematical operators : &、+ ( Character connector )、+( Add )、-( reduce )、*( ride )、/( except )、Mod( Remainder )、\( to be divisible by )、-( Minus sign )、^( Index )
(3) Logical operators :Not( Not )、And( And )、Or( or )、Xor( Exclusive or )、Eqv( equal )、Imp( Implication )
(4) Relational operator : = ( equal )、<>( Unequal )、>( Greater than )、<( Less than )、>=( Not less than )、<=( No more than )、Like、Is
(5) An operator Not( Logic is not )、And( Logic and )、Or( Logic or )、Xor( Logical XOR )、Eqv( Logic, etc. )、Imp( Implication )
2、 data type :
VBA share 12 Type of data , See the following table for details , In addition, users can also use... According to the following types Type Custom data types .
data type | Type identifier | byte |
String type String | $ | Character length (0-65400) |
Byte type Byte | nothing | 1 |
Boolean type Boolean | nothing | 2 |
Integer type Integer | % | 2 |
Long integer type Long | & | 4 |
Single precision type Single | ! | 4 |
Double precision type Double | # | 8 |
Date type Date | nothing | 8 A.D. 100/1/1-9999/12/31 |
Currency type Currency | @ | 8 |
Decimal point type Decimal | nothing | 14 |
Variant Variant | nothing | Any of the above types , variable |
Object type Object | nothing | 4 |
3、 Variables and constants
(1)VBA Allow undefined variables , Variant variable by default Variant.
(2) In the general description of the module , Join in Option Explicit Statement can force the user to define variables .
(3) Variable definition statement and variable scope
Dim Variable as type | ' Defined as a local variable | Such as Dim xyz as integer |
Private Variable as type | ' Define as private variable | Such as Private xyz as byte |
Public Variable as type | ' Defined as a public variable | Such as Public xyz as single |
Global Variable as type | ' Defined as a global variable | Such as Globlal xyz as date |
Static Variable as type | ' It's defined as a static variable | Such as Static xyz as double |
The principle of the general variable scope is , That part of the definition works in that part , The definition in the module is used in the module .
(4) Constant is a special case of variable , use Const Definition , And assigned when defined , Value cannot be changed in program , Scope is also like variable scope .
As defined below :Const Pi=3.1415926 as single
4、 Array
In memory, it appears as a continuous memory block , Must use Global or Dim To define . The rules of definition are as follows :Dim Array name ([lower to ]upper [, [lower to ]upper, ….]) as type ;Lower The default value is 0. Two dimensional arrays are arranged in rows and columns , Such as XYZ( That's ok , Column ). In addition to the above fixed array ,VBA There is also a powerful dynamic array , There is no size dimension declaration when defining ; Reuse in the program Redim Statement to resize the array , The contents of the original array can be added preserve Keywords to keep . Here's an example :Dim array1() as double : Redim array1(5) : array1(3)=250 : Redim preserve array1(5,10)
5、 notes
Comment statement is used to explain the function and function of some statements in the program ;VBA There are two ways to identify them as comment statements in . Single quotation marks ’ ; Such as :’ Define global variables ; Can be at the end of other statements , It can also be a single line Rem ; Such as :Rem Define global variables ; Only one line .
6、 Writing norms
(1)VBA Don't distinguish the case of the identifier , It's all lowercase ;
(2) You can write multiple sentences in one line , Colon between statements : Separate ;
(3) A statement can be written in multiple lines , Underline with spaces _ To identify line continuation ;
(4) It's best to keep the identifier simple , No ambiguity .
In the second quarter : Common control statements and examples
1、 Judgment statement If … Then … Else Endif sentence
If a>60 Then
k=1
Elself a>70 Then
k=2
Elself a>80 Then
k=3
Elself a>90 Then
k=4
Endif2、Select Case sentence
Select Case Time
Case 1 To 11
msg = " In the morning "
Case 12
msg = " At noon, "
Case 13 To 16
msg = " Afternoon "
Case 17 To 20
msg = " evening "
Case 23, 24
msg = " The middle of the night "
Case Else
msg = " Unknown "
End Select
MsgBox " Now it is :" & msg3、 Loop statement
(1)For … Next loop
For cnt = 1 To 10 Step 1 ' establish 10 Secondary cycle
For Chars = 0 To 9 ' establish 10 Secondary cycle
MyString = MyString & Chars ' Add a number to the string
Next Chars ' Increment counter
MyString = MyString & " " ' Add a space
Next cnt
[Exit For](2)For Each…Next sentence
The main function is to set an array or collection object , Let all elements execute the statement again
Sub Greater than 90 Cells of are painted red ()
Dim rng As Range
For Each rng In Sheet5.Range("b3:b12")
Select Case rng
Case Is >= 90
rng.Interior.ColorIndex = 3
End Select
Next
End SubOther common cycles
1) Do While loop
---------------------------------------------------------
Do [While Conditions ]
[ Instructions ]
[Exit Do]
[ Instructions ]
Loop
---------------------------------------------------------
Do
[ Instructions ]
[Exit Do]
[ Instructions ]
Loop [While Conditions ]
---------------------------------------------------------
2) Do Until loop
---------------------------------------------------------
Do [Until Conditions ]
[ Instructions ]
[Exit Do]
[ Instructions ]
Loop
---------------------------------------------------------
Do
[ Instructions ]
[Exit Do]
[ Instructions ]
Loop [Until Conditions ]
---------------------------------------------------------
3) While … Wend loop
While < Conditions >
[ Instructions ]
Wend
边栏推荐
- Amazoncaptcha bypasses Amazon IP verification code with 95% success rate
- HTB-Devel
- Promise implementation
- Summer summary 2
- (2022牛客多校二)K-Link with Bracket Sequence I(动态规划)
- leetcode/二进制加法
- An SQL execution process
- 10. Rendering Basics
- ECS is exclusive to old users, and the new purchase of the remaining 10 instances is as low as 3.6% off
- Unity model simplification / consolidation one click plug-in
猜你喜欢

Unity Animator动画与状态机

(Niuke multi school I in 2022) i-chiitoitsu (expected DP)
![[node] the service port is occupied error: listen eaddinuse: address already in use::: 9000- how to close the port started by node](/img/06/b90fa310158669696f94f79ef4cc5a.png)
[node] the service port is occupied error: listen eaddinuse: address already in use::: 9000- how to close the port started by node
This is how the permission system is designed, yyds

Use abp Zero builds a third-party login module (4): wechat applet development

Big talk · book sharing | Haas Internet of things device cloud integrated development framework

【每日一练】day(14)

(2022牛客多校二)L-Link with Level Editor I(动态规划)

New developments in Data Governance: what is the impact of the EU's Data Governance Research Report on China

SAP FICO section III BDC and ltmc import S4 financial account
随机推荐
R language obtains the data row where the nth maximum value of the specified data column is located in the data.table data
Design of automatic machine dot drawing script based on C language
[node] the service port is occupied error: listen eaddinuse: address already in use::: 9000- how to close the port started by node
ABC 261.D - Flipping and Bonus ( DP )
npx和npm区别
剑指 Offer 36. 二叉搜索树与双向链表
Get URL of [url reference]? For the following parameters, there are two ways to get the value of the corresponding parameter name and convert the full quantity to the object structure
(2022牛客多校二)L-Link with Level Editor I(动态规划)
Sword finger offer 54. the k-th node of the binary search tree
嵌入式c语言开发之宏定义求两个数的最大值的使用技巧
Equal proportion of R language test group: use the prop.test function to test whether the success proportion of the two groups is equal
VBA common objects
[QT] solve the problem of Chinese garbled code output from QT console
Draw Bezier curve through screen interaction
同条网线电脑正常上网,手机连接wifi成功,但是无法访问互联网
Msys2 common configuration
context must be a dict rather解决
Blocking Queue Analysis
HTB-Devel
计算BDP值和wnd值