VBA/Excel/Access/Word/Data Type/Integer

Материал из VB Эксперт
Перейти к: навигация, поиск

Add integer together

   <source lang="vb">

Sub Accumulate()

   Dim n As Integer
   Dim t As Integer
   For n = 1 To 10
       t = t + n
   Next n
   MsgBox "        The total is " & t

End Sub

</source>
   
  


Add two numbers together

   <source lang="vb">

Sub addNumbers()

  "Declare the variables
  Dim intNumber1 As Integer
  Dim intNumber2 As Integer
  Dim intSum As Integer
  "Create InputBoxes to enter numbers
  intNumber1 = InputBox("Enter the first number")
  intNumber2 = InputBox("Enter the second number")
  "Add numbers
  intSum = intNumber1 + intNumber2

" Create an output

  Debug.Print "The numbers entered were " & intNumber1 & " and " & intNumber2

End Sub

</source>
   
  


Input and Output

   <source lang="vb">

Sub addNumbers()

  Dim intNumber1 As Integer
  Dim intNumber2 As Integer
  Dim intSum As Integer
  "Create InputBoxes to enter numbers
  intNumber1 = InputBox("Enter the first number")
  intNumber2 = InputBox("Enter the second number")

End Sub

</source>
   
  


Integer number

   <source lang="vb">

Sub MyNumber()

       Dim intNum As Integer
   
       intNum = 23.11
       MsgBox intNum

End Sub

</source>
   
  


Local Integer variable

   <source lang="vb">

Public Sub TestLocal1()

   Dim intVariable1    As Integer
   intVariable1 = intVariable1 + 1
   Debug.Print intVariable1

End Sub

</source>
   
  


Overflow error

   <source lang="vb">

Sub macro_overflow()

 Dim l As Integer
 l = 255 * 256   "overflow error

End Sub Sub macro_no_overflow()

 Dim l As Long
 l = 255& * 256  "now it works

End Sub

</source>
   
  


Select Case statement with Integer value

   <source lang="vb">

Sub cmdCase_Click()

   Dim intAge As Integer
   intAge = 12
   Select Case intAge
     Case 0
       MsgBox "You Must Enter a Number"
     Case 1 To 18
       MsgBox "You Are Just a Kid"
     Case 19, 20, 21
       MsgBox "You are Almost an Adult"
     Case 22 To 40
       MsgBox "Good Deal"
     Case Is > 40
       MsgBox "Getting Up There!"
     Case Else
       MsgBox "You Entered an Invalid Number"
   End Select

End Sub

</source>
   
  


==The Integer data type is the most efficient way of handling numbers from -o /td>




   <source lang="vb">

Sub intDemo()

   Dim intMyVar As Integer
   For intMyVar = 1 To 300
       "repeat actions
   Next intMyVar

End Sub

</source>
   
  


Use If statement with Integer

   <source lang="vb">

Sub ifTest()

   Dim intNum As Integer
   Dim strMessage As String
   intNum = 12
   If intNum > 10 Then
       strMessage = "The number is " & intNum
   End If
   Debug.Print strMessage

End Sub

</source>