VBA列數字與字母互換

2021-08-15 05:43:02 字數 3068 閱讀 3276

《內容來至excelhome>

vba列數字與字母互換:

方案一:速度:快

function numtostr(byval num as long) as string   '數字轉字母

dim m as long

if num < 1 then exit function

dom = num mod 26

if m = 0 then m = 26

numtostr = chr(64 + m) & numtostr

num = (num - m) / 26

loop until num <= 0

end function

function strtonum(byval str as string) as long '字母轉數字

dim s as string, s1 as string

if str = "" then exit function

str = ucase(str)

s = "abcdefghijklmnopqrstuvwxyz"

for i = 1 to len(str)

s1 = mid(str, i, 1)

if instr(s, s1) = 0 then crazy_num = -1: exit function

strtonum = strtonum + instr(s, s1) * 26 ^ (len(str) - i)

next

end function

方案二:迭代 速度:略快 

function colinf(t) '列標籤字母⇔列序號數值 相互轉換

if t = "" then colinf = "": exit function '如果引用単元格=空白 即返回空白退出

if isnumeric(t) then '如果引用単元格=數值(列序號),計算返回列標籤大寫英文本母

if t < 1 then colinf = "": exit function '如果數是0或負數返回空白退出

docolinf = chr((t - 1) mod 26 + 65) & colinf '以26除數反覆mod求餘得到1-26字母

t = int((t - 1) / 26) '求餘後再用26除后int取整……

loop until t <= 0 '反覆迭代計算直至數t已不能除26求餘止

else '如果引用単元格=文字字元,計算返回序號數値

t = ucase(t) '首先文字転換大寫字母

for i = 1 to len(t) '遍各字元

colinf = colinf + (asc(mid(t, i, 1)) - 64) * 26 ^ (len(t) - i) '按26冪乗計算累計數

next

end if

end function

方案三:遞迴 速度:略快 

function getcolstr$(n) '列序號數字→列標籤字母

dim s$, t as long

t = n

dos = chr((t - 1) mod 26 + 65) & s

t = int((t - 1) / 26)

loop until t <= 0

getcolstr = s

end function

方案四:

function getc_str(n) '列序號數字→列標籤字母

getc_str = split(cells(1, n).address, "$")(1)

end function

------------------以下為測試3種方案全碼-----------------

sub test()

dqt = timer

m = 1000: n = 10000

dim strtmp

for i = 1 to m

for j = 1 to n

strtmp = getc_str(n)

next j

next i

dqt1 = timer

for i = 1 to m

for j = 1 to n

strtmp = getcolstr(j)

next j

next i

dqt2 = timer

for i = 1 to m

for j = 1 to n

strtmp = numtostr(n)

next j

next i

msgbox " getc_str  用時:" & format(timer - dqt1, "0.0000") & vblf & "getcolstr 用時:" & format(dqt2 - dqt1, "0.0000") & vblf & "numtostr用時:" & format(timer - dqt2, "0.0000")

end sub

function getcolstr$(n) '列序號數字→列標籤字母

dim s$, t as long

t = n

dos = chr((t - 1) mod 26 + 65) & s

t = int((t - 1) / 26)

loop until t <= 0

getcolstr = s

end function

function numtostr(byval num as long) as string   '數字轉字母

dim m as long

if num < 1 then exit function

dom = num mod 26

if m = 0 then m = 26

numtostr = chr(64 + m) & numtostr

num = (num - m) / 26

loop until num <= 0

end function

function getc_str(n) '列序號數字→列標籤字母

getc_str = split(cells(1, n).address, "$")(1)

end function

python 字母與數字

給定乙個放有字元和數字的陣列,找到最長的子陣列,且包含的字元和數字的個數相同。返回該子陣列,若存在多個最長子陣列,返回左端點最小的。若不存在這樣的陣列,返回乙個空陣列。示例 1 輸入 a 1 b c d 2 3 4 e 5 f g 6 7 h i j k l m 輸出 a 1 b c d 2 3 4...

使用Java將Excel 列號數字與字母互相轉換

public class excelcolumn 該方法用來將excel中的abcd列轉換成具體的資料 param column abcd列名稱 return integer 將字母列名稱轉換成數字 public static int excelcolstrtonum string column r...

Excel列數與對應字母的轉化

最近遇到這樣個問題,將資料寫進excel文件中,在插入公式的時候希望得到某列對應的字母,如給第一列對應 a 第52列對應 az 現將 貼出,後面待用 public string getexcelcolumnletter int column else columnletter char 64 col...