有个已做好的excel销售订单格式,需要在填写信息完成后保存自动生成一个单据编码,形式为201204000X

2024-11-27 02:26:08
推荐回答(2个)
回答1:

如果每次打开表格时此单据码自己改变,可以不用VBA,只用EXCEL公式就行了,比如在A1格中生成一个包括日期的编码,可以在A1中输入公式=YEAR(NOW()) & RIGHT("00"&MONTH(NOW()),2) & RIGHT("00"&DAY(NOW()),2) & "001"
如果想每次打开表格后,此单据码是不能改变的,就要用VBA了,在VBA窗口中左侧找到Thisworkbook项目,输入下面代码:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets("sheet2").Range("a1") = Year(Now()) & Right("00" & Month(Now()), 2) & Right("00" & Day(Now()), 2) & "001"
End Sub

回答2:

Sub setRndNum()
t = Timer
Randomize
Dim myArr
Range("a1") = 1
Range("a1").DataSeries 2, , , 1, 10000
myArr = Range("a1:a10000")

For i = 1 To 10000
a = Int(Rnd() * 10000) + 1
temp = myArr(i, 1)
myArr(i, 1) = myArr(a, 1)
myArr(a, 1) = temp
Next

Range("a1:a10000") = myArr
[c1] = Timer - t
End Sub

这段代码是从别处抄来的,不明白可以去搜如何生成随机数