2016-09-05 6 views
4

私は6桁の数字に3桁の長さで進数を変換したいです。例えば、12Fは00012Fです。私はこのコードを試みたが、それは仕事をしなかった。エクセルVBAの書式進

endadd = Format(endadd, "000000") 
+10

は 'endadd =右試してみてください( "000000" &endadd、6)それは魔法のように動作' –

+1

感謝。乾杯! – Manick9

答えて

1

スコットCranerが指摘したように、簡単なRight("000000" & endadd,6)は完璧にうまく動作しますが、Right$("000000" & endadd,6)はわずかに速いです。

また、パフォーマンスの観点から、それが本当にendadd値の元のソースが文字列または数値であるかどうかに依存します。 10メートルの操作ループから

'CONCAT String Approach 
'If the hex source is a string, then this is the most efficient approach 
formattedHex = Right$("000000" & "12F", 2) 

'CONCAT Numeric Approach 
'But if the hex source is a numeric, then this hex conversion AND concatenation is required, but it is SLOW 
formattedHex = Right$("000000" & Hex$(&H12F), 2) 

'ADDITION/OR Numeric Approach 
'When the hex source is numeric it is more efficient to use a bit trick to add AND convert 
formattedHex = Right$(Hex$(&H1000000 + &H12F), 2) 
formattedHex = Right$(Hex$(&H1000000 Or &H12F), 2) 

結果:

Approach     | Using Right | Using Right$ | 
==========================+============================= 
CONCAT String Approach | 1.59s  | 1.40s 
CONCAT Numeric Approach | 2.63s  | 2.33s 
ADDITION Numeric Approach | 1.74s  | 1.60s 
======================================================