作者:绿色风
分类:
时间:2022-08-17
浏览:93
楼主 wise |
Q:如何用VBA返回所有5位数的回文数? 回文数,如12321是回文数,个位和万位数字相同,十位和千位相同 A:ALT+F11→插入模块→输入下面代码:
- Sub 回文数()
- Dim m As Long, n As Long, i As Long, j As Long, r As Long
- r = 1
- For k = 10000 To 99999
- m = Int(k / 10000)
- n = Int((k - m * 10000) / 1000)
- i = CInt(Left(Right(CStr(k), 2), 1))
- j = CInt(Right(CStr(k), 1))
- If (m = j) And (n = i) Then
- Cells(r, 1) = k
- r = r + 1
- End If
- Next k
- End Sub
|
2楼 biaotiger1 |
踩一脚- Sub 回文数2()
- For i = 1 To 9
- For j = 0 To 9
- For k = 0 To 9
- r = r + 1
- Cells(r, 2) = i * 10000 + j * 1000 + k * 100 + j * 10 + i
- Next k
- Next j
- Next i
- End Sub
|
3楼 DJ_Soo |
操作字符串太多啦,比我这个还慢了不少- Sub 回文数()
- Dim a As Byte, b As Byte, c As Byte
- Dim i As Integer
- For a = 1 To 9
- For b = 0 To 9
- For c = 0 To 9
- i = i + 1
- Cells(i, 2) = a & b & c & b & a
- Next c
- Next b
- Next a
- End Sub
|
4楼 wise |
好多答案啊,看来我真的很菜 |
5楼 DJ_Soo |
哈哈,你的规律这么简单,干嘛操作这么繁琐呢? 直接当成字符连接起来就行了,反正送到cells里的时候会自动当成数字的. |
6楼 rongjun |
这样不好吗?搞这么多循环?- Sub 回文数()
- Dim r, k As Long
- r = 1
- For k = 10000 To 99999
- If k = StrReverse(k) Then
- Cells(r, 1) = k
- r = r + 1
- End If
- Next k
- End Sub
|
7楼 biaotiger1 |
荣版高,回文放在这里最恰当不过啦 |
8楼 rongjun |
改一下,速度就快了:- Sub 回文数()
- Dim r, k As Long
- r = 1
- For k = 100 To 999
- Cells(r, 1) = Left(k, 2) & StrReverse(k)
- r = r + 1
- Next k
- End Sub
|
9楼 水星钓鱼 |
各显神通 |
免责声明
有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素,
经与ExcelTip.Net站长Apolloh商议并征得其同意,
现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示,
供有需要的人士查询使用,也慰缅曾经的论坛时代。
所示各个帖子的原作者如对版权有异议,
可与本人沟通提出,或于本站点留言,我们会尽快处理。
在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一