自定义函数
=ZS(ROW(A1))
下拉
【EXCEL自定义函数求1000以内的质数】自定义函数代码
Function ZS(s%)
Dim f As Boolean, arr(), a%, i%
ReDim Preserve arr(1)
arr(0) = 2
For i = 3 To 999 Step 2
For j = 2 To i – 1
If i Mod j = 0 Then f = True: Exit For
Next
If Not f Then
a = a + 1
ReDim Preserve arr(a)
arr(a) = i
End If
f = False
Next
ZS = arr(s – 1)
End Function
宏代码
Sub getZS()
Dim arr(), f As Boolean
ReDim Preserve arr(1)
arr(0) = 2
For i = 3 To 999 Step 2
For j = 2 To i – 1
If i Mod j = 0 Then f = True: Exit For
Next
If Not f Then
a = a + 1
ReDim Preserve arr(a)
arr(a) = i
End If
f = False
Next
[c2].Resize(a, 1) = Application.Transpose(arr)
End Sub
推荐阅读
- excel vba中Vlookup返回多个值 自定义函数
- Excel怎么统计单元格内字符出现次数
- excel Vlookup函数第四个参数等于1时的重要应用
- Excel VBA过程和函数使用基础价赔偿
- 怎么拆分Excel单元格日期与时间?
- Excel VBA一个简单例子,VBA快速入门
- excel VBA标识符
- 怎么获取当前时区并在Excel中的单元格中显示?
- excel实现按颜色求和与计数函数
- 利用excel宏命令完成财务指标的计算