Dim Words, Chars, MyString
For Words = 10 To 1 Step -1' 建立 10 次循环。
For Chars = 0 To 9 ' 建立 10 次循环。
MyString = MyString &Chars ' 将数字添加到字符串中。
Next Chars ' Increment counter
MyString = MyString &" " ' 添加一个空格。
Next Words
2.
本示例示范如何使用 Do...Loop 语句。内层的 Do...Loop 语句循环到第 10 次时将标志值设置为 False,并用 Exit Do 语句强制退出内层循环。外层循环则在检查到标志值为 False 时,马上退出。
Dim Check, Counter
Check = True: Counter = 0 ' 设置变量初始值。
Do ' 外层循环。
Do While Counter <20 ' 内层循环。
Counter = Counter + 1 ' 计数器加一。
If Counter = 10 Then ' 如果条件成立。
Check = False ' 将标志值设成 False。
Exit Do ' 退出内层循环。
End If
Loop
Loop Until Check = False ' 退出外层循扮枣环。
3.
While...Wend 语句示例
本示例使用 While...Wend 语句来增加计数变量的值。如果条件判断值为 True,则循环内的语句将一直执行下去。
Dim Counter
Counter = 0 ' 设置变量初值。
While Counter <20 ' 测试计数器的值。告缺局
Counter = Counter + 1 ' 将计数器的值加一。
Wend ' 当 Counter >19 时则循环终止。
Debug.Print Counter ' 在“立即”窗口中显示数字 20。
Select Case 语句示例
本示例使用 Select Case 语句来判断变量的值。示例中第二个 Case 子句包含了变量值,故只有此区块内的语句会被完成到。袜让
Dim Number
Number = 8 ' 设置变量初值。
Select Case Number ' 判断 Number 的值。
Case 1 To 5 ' Number 的值在 1 到 5 之间,包含1 和 5 。
Debug.Print "Between 1 and 5"
' 下一个 Case 子句是本示例中唯一判断值为 True 的子句。
Case 6, 7, 8 ' Number 的值在 6 到 8 之间。
Debug.Print "Between 6 and 8"
Case 9 到 10 ' Number 的值为 9 或 10。
Debug.Print "Greater than 8"
Case Else ' 其他数值。
Debug.Print "Not between 1 and 10"
End Select
Private Sub Form_Activate()'1年期2.25%,2年期2.43%,3年期为2.70%,5年期为2.88%(不记复利)。今有X元,5年以后使用,共有如下6中存法:
Const x1 As Double = 0.0225, x2 As Double = 0.0243, x3 As Double = 0.027, x5 As Double = 0.0288
Dim x As Double
x = Val(InputBox(""))
Print "①存一次5年期,5年后到期的本息合计"ss(x, 5, x5)
Print "②存一次3年期,一次2年期,5年后到期的本息合计"ss(ss(x, 3, x3), 2, x2)
Print "③存郑蚂一次3年期,两次1年喊橡埋期,5年后到期的本息合计"ss(ss(ss(x, 3, x3), 1, x1), 1, x1)
Print "④存两次2年期,一次1年期,5年后到期的本息合计"ss(ss(ss(x, 2, x2), 2, x2), 1, x1)
Print "⑤存一次2年期,三次1年期,5年后到期的本如碰息合计"ss(ss(ss(ss(x, 2, x2), 1, x1), 1, x1), 1, x1)
Print "⑥存五次1年期,5年后到期的本息合计"ss(ss(ss(ss(ss(x, 1, x1), 1, x1), 1, x1), 1, x1), 1, x1)
End Sub
Private Function ss(ByVal a As Double, b As Integer, c As Double) As Double
ss = a + a * c * b
End Function
Private Sub Form_Load()
AutoRedraw = True
End Sub
将下列代码复制到一个文本文件,并将文本文件命名为Form1.frm,然后运行(我不知道你要取消按钮的用途)VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight= 4035
ClientLeft = 60
ClientTop = 450
ClientWidth = 5310
LinkTopic = "Form1"
ScaleHeight = 4035
ScaleWidth = 5310
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox Text1
BeginProperty Font
Name= "宋体"
Size= 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 465
Left= 1920
TabIndex= 3
Top = 240
Width = 2175
End
Begin VB.CommandButton Command2
Caption = "取 消"
BeginProperty Font
Name= "宋体"
Size= 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Left= 3000
TabIndex= 1
Top = 2400
Width = 1455
End
Begin VB.CommandButton Command1
Caption = "确 定"
Default = -1 'True
BeginProperty Font
Name= "宋体"
Size= 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Left= 720
TabIndex= 0
Top = 2400
Width = 1455
End
Begin VB.Label Label3
BeginProperty Font
Name= "宋体"
Size= 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left= 1800
TabIndex= 5
Top = 1080
Width = 2295
End
Begin VB.Label Label2
Caption = "八位号"
BeginProperty Font
Name= "宋体"
Size= 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left= 480
TabIndex= 4
Top = 1080
Width = 1215
End
Begin VB.Label Label1
Caption = "姓 名"
BeginProperty Font
Name= "宋体"
Size= 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left= 480
TabIndex= 2
Top = 360
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Sub Command1_Click()
If Text1 = "" Then
MsgBox "请输入名字!"
Text1.SetFocus
Exit Sub
End If
Randomize
CF:
Label3 = LTrim(Str(Int(Rnd() * 100000000)))
Label3 = String(8 - Len(Label3), "0") &Label3
Open "D:\花名册\花名册.txt" For Input As #1
Do While Not EOF(1)
Input #1, strzc
If strzc = "" Then GoTo TG
k = Split(strzc, " ")
If k(1) = Label3 Then
Close #1
GoTo CF
End If
If k(0) = Text1 Then
w = MsgBox("输入的姓名已经存在,点“是”保存,点“否”放弃。", vbYesNo, "提示")
If w = 7 Then
Close #1
Exit Sub
End If
End If
Loop
TG:
Close #1
Open "D:\花名册\花名册.txt" For Append As #1
Print #1, Text1 &" " &Label3 &" " &Format(Now, "yyyy年mm月dd日hh:nn:ss")
Close #1
Text1.SelStart = 0
Text1.SelLength = Len(Text1)
End Sub
Private Sub Form_Load()
If Dir(App.Path &"\MyTime.INI") = "" Then
MkDir "D:\花名册"
Open "D:\花名册\花名册.txt" For Append As #1
Close #1
End If
A = WritePrivateProfileString("TIP", "START", Time$, App.Path &"\MyTime.INI")
Show
Text1.SetFocus
End Sub
更多VB代码请参阅我的博客:http://hi.baidu.com/zgmg/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)