EXCEL中的这个宏代码怎么写

2024-11-28 01:42:05
推荐回答(2个)
回答(1):

Sub xabc()
Randomize
Dim s(3, 3)
Dim t As Integer
t = 350
s(0, 0) = 0
s(0, 1) = 0
s(0, 2) = 0
s(1, 0) = 0.2
s(1, 1) = 0.2
s(1, 2) = 0.6
s(2, 0) = Int(t * s(1, 0))
s(2, 1) = Int(t * s(1, 1))
s(2, 2) = t - s(2, 1) - s(2, 0)
i = 1
Do While True
m = Rnd()
If m < s(1, 0) And s(0, 0) < s(2, 0) Then
Cells(i, 1) = "A"
s(0, 0) = s(0, 0) + 1
i = i + 1
Else
If m < s(1, 1) + s(1, 0) And s(0, 1) < s(2, 1) Then
Cells(i, 1) = "B"
s(0, 1) = s(0, 1) + 1
i = i + 1
Else
If m < s(1, 2) + s(1, 0) + s(1, 1) And s(0, 2) < s(2, 2) Then
Cells(i, 1) = "C"
s(0, 2) = s(0, 2) + 1
i = i + 1
End If
End If
End If
If i > t Then Exit Do
Loop
End Sub
'定义的三维数组,第一维用来计数,记下A、B、C各生成了多少
第二维是A、B、C所占的比重,如果在三个空格里输入比例,让这儿分别等于三个单元格的值。
第三维是计算出来的A、B、C各自应该多少个。
t=350是总数
每个位置是什么由随机数来决定。

回答(2):

Sub aaaa()
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Sheets("sheet1").Select
Range("a:b").Select
Selection.Copy
Sheets("sheet3").Select
Range("a:b").Select
ActiveSheet.Paste
a = WorksheetFunction.CountA(Range("a:a"))
b = WorksheetFunction.CountA(Range("b:b"))
Sheets("sheet2").Select
c = WorksheetFunction.CountA(Range("a:a"))
d = WorksheetFunction.CountA(Range("b:b"))
Range("a1:" & "a" & c).Select
Selection.Copy
Sheets("sheet3").Select
Range("a" & a + 1).Select
ActiveSheet.Paste
Sheets("sheet2").Select
Range("b1:" & "b" & d).Select
Selection.Copy
Sheets("sheet3").Select
Range("b" & b + 1).Select
ActiveSheet.Paste
End Sub