按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
v = Int((5 * Rnd) + 1)
Cells(i; 1) = v
Next
End Sub
解答 3:是这样的:我有无数的随机数,是由 rand()产生的,(这是以前输的,我不想
去重输这么多)这是前提。
当我获得一组合适的值的时候,我就在 A1 格内输入 1,从而使随机数固定在那组满意的
值。下一组时,我在 A1 格输入 0。就是这样。
解答 4:可不可以把满意的那组贴成数值到新表?当然是用 vba 来实现的。
Sub g()
Sheets(1)。UsedRange。Copy
Worksheets。Add after:=Worksheets(Worksheets。Count)
Sheets(Worksheets。Count)。Cells(1; 1)。Select
Selection。PasteSpecial Paste:=xlvalues
Sheets(1)。Cells(1; 1) = 1
End Sub
解答 5:
Private Sub mandButton1_Click()
Dim a; b As Integer
For a = 1 To 2 '产生第一次随机数
For b = 1 To 3
Cells(a; b) = Int((8 * Rnd) + 1)
405
…………………………………………………………Page 406……………………………………………………………
Next b
Next a
Dim c As Integer
For c = 1 To 10 ’询问是否对随机数满意
If (MsgBox(〃did u think the random are ok?〃; 260)) = vbYes Then
Cells(3; 1) = 1 '如果需要一个 0 或 1 的单元格给其它程序块使用
Exit Sub '满意就退出
Else '不满意就继续产生随机数
For a = 1 To 2
For b = 1 To 3
Cells(a; b) = Int((8 * Rnd) + 1)
Next b
Next a
Cells(3; 1) = 0
End If
Next c
MsgBox 〃if u want to continue rondom number press cmd1 again please〃
End Sub
解答 6 简化一下:
Sub test()
Upper = 0
Bottom = 100
Do While Range(〃A1〃) 1
Range(〃E1〃) = Int(Rnd() * (Upper Bottom + 1)) + Bottom
Response = MsgBox(〃Do you accept the number?〃; vbYesNo + vbDefaultButton2)
If Response = vbYes Then Range(〃A1〃) = 1
Loop
End Sub
406
…………………………………………………………Page 407……………………………………………………………
排列组合
比如现在有一个 长度是 9 位的字符串 (ABCDEFGHI),想列出全部的只取其中 7个字符的组合值:
CDEFGHI、ADEFGHI、ABEFGHI、ABCFGHI、ABCDGHI、〃〃、共 36 个。用函数或 VBA 均可。
解答:Sub bination()
Dim a; b; c; d; e; f; g; h; i; j; k As Integer
Dim str As String
j=1
For a = 0 To 1
For b = 0 To 2 Step 2
For c = 0 To 3 Step 3
For d = 0 To 4 Step 4
For e = 0 To 5 Step 5
For f = 0 To 6 Step 6
For g = 0 To 7 Step 7
For h = 0 To 8 Step 8
For i = 0 To 9 Step 9
k = a / 1 + b / 2 + c / 3 + d / 4 + e / 5 + f / 6 + g / 7 + h / 8 + i / 9
If k = 7 Then
str = 〃〃
If a 0 Then str = str & 〃A〃
If b 0 Then str = str & 〃B〃
If c 0 Then str = str & 〃C〃
If d 0 Then str = str & 〃D〃
If e 0 Then str = str & 〃E〃
If f 0 Then str = str & 〃F〃
If g 0 Then str = str & 〃G〃
If h 0 Then str = str & 〃H〃
If i 0 Then str = str & 〃I〃
cells(j;1)=str
j=j+1
End If
Next i
Next h
Next g
Next f
Next e
Next d
Next c
Next b
Next a
End Sub
如用 MID 函数,修改一下以上程序可得到任意长度为 9 的字符串的任取 7 个字符的组合。
解答 2:用公式的解:
407
…………………………………………………………Page 408……………………………………………………………
=REPLACE(REPLACE(M1;MIN(IF(ROW() 4 Then
。Value = 〃〃
GoTo ex
End If
If Trim(Target) = 〃〃 Then
If Trim(。Offset(1; 0)) = 〃〃 Then
If 。Row = 5 Then
。Offset(…1; 0) = 〃〃
ElseIf 。Row 》 5 Then
。Offset(…1; 0) = 〃=SUM(〃 & 。Offset(…1; 0)。End(xlUp)。Address(False; False; xlA1) & 〃:〃
& 。Offset(…2; 0)。Address(False; False; xlA1) & 〃)〃
End If
End If
If 。Offset(1; 0)。HasFormula And 。Row = 4 Then 。O