2.1 Data types
Data type |
Occupied bytes |
Byte |
1 |
Boolean |
2 |
Integer |
2 |
Long |
4 |
Single |
4 |
Double |
8 |
Currency |
8 |
Decimal |
14 |
Date |
8 |
Object |
4 |
String |
10 |
Variant |
16 |
Instance
Sub Test ()
Dim sums as long, cell as range, I As Integer, mystr as String
For each cell in range ("A1:A10")
If vba.isnumeric (cell) then sums = sums + cell Else MyStr = mystr & Cell
If cell = "Then i = i +1
Next cell
Debug.Print "Blank cells in a1:a10" & I & "X"
Debug.Print "A1:a10 in the data and for:"; Sums
Debug.Print "A1:a10 in the text as:"; MyStr
End Sub
2.2 Data type Conversions
Function |
return type |
CBool |
Boolean |
CByte |
Byte |
Ccur |
Currency |
Cdate |
Date |
Cbdl |
Double |
Cdec |
Decimal |
Cint |
Integer |
Clng |
Long |
Csng |
Single |
Cstr |
String |
Cvar |
Variant |
Instance:
Sub type conversion ()
Dim funds as Double
Funds = 80.42454
Msgbox "Type:" & TypeName (Funds) & "value:" & Funds ' TypeName () for identifying data types
Msgbox "Type:" & TypeName (CBool (funds)) & "Value:" & CBool (Funds)
Msgbox "Type:" & TypeName (CByte (funds)) & "Value:" &cbyte (funds)
End Sub
2.3 Variables
Instance:
Sub Display user name ()
user_name = Application.inputbox ("Please enter your name", "name",,,,,, 2)
Msgbox user_name
End Sub
Dim temp As Byte ' before sub Dim, This variable is a module-level variable, or declared with public
Sub A ()
Temp = 10
Msgbox Temp
End Sub
Sub B ()
Temp = temp + 10
Msgbox Temp
End Sub
Sub A ()
Dim temp = As Byte Dim within sub, variable within two sub is not relevant, process level variable
Temp = 10
Msgbox Temp
End Sub
Sub B ()
Dim Temp As Byte
Temp = 20
Temp = temp + 10
Msgbox Temp
End Sub
Let A = 10 ' assigns A value to A non-object variable
Set A = Range ("A10") ' assigns A value to A Cell object
Set A = [A10]
Sub setting A10 font ()
ActiveWorkbook.Sheets ("Sheet2"). Range ("A10"). Font.Name = "Blackbody"
ActiveWorkbook.Sheets ("Sheet2"). Range ("A10"). font.colorindex= 3
ActiveWorkbook.Sheets ("Sheet2"). Range ("A10"). Interior.ColorIndex = 5
End Sub
Sub setting A10 font two ()
Dim RNG as Range
Set rng = activeworkbook.sheets ("Sheet2"). Range ("A10")
Rng.font.name = "Blackbody" ' Sets the Cell object, the program simplifies a lot and improves efficiency
Rng.font.colorindex = 3
Rng.interior.colorindex = 5
End Sub
2. In-depth analysis of data types and variables--"Excel VBA program Development Self-study treasure"