作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 VBScript 获取一个数字并将其转换为小写 a-z
字母。
例如:
1
转换为 a
2
转换为 b
27
转换为 aa
28
转换为 ab
特别是,在转换为 2 个字母的单元格名称时,我在转换 26 之后的数字时遇到问题。 (aa、ab、ac 等)
最佳答案
您应该看看 Chr(n)
功能。
这将满足您从 a
到 z
的需求:
wscript.echo Chr(number+96)
要表示数字的多个字母(就像 Excel 那样),您必须检查数字的范围并使用 Mod
求模运算符。
编辑:
网上有一个快餐复制粘贴的例子:How to convert Excel column numbers into alphabetical characters
For example: The column number is 30.
The column number is divided by 27:
30 / 27 = 1.1111
, rounded down by the Int function to "1".i = 1
Next Column number - (i * 26) =
30 -(1 * 26) = 30 - 26 = 4
.j = 4
Convert the values to alphabetical characters separately,
i = 1 = "A"
j = 4 = "D"Combined together, they form the column designator "AD".
及其代码:
Function ConvertToLetter(iCol As Integer) As String
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha > 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder > 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function
关于vbscript - 如何在VBS中将数字转换为字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38795187/
我是一名优秀的程序员,十分优秀!