2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Excelで数値の先頭に0をつける

Posted at

なにかしらのキー(4桁以上7桁未満)が利便的にまとめられているけど、
先頭4文字(先頭から0を含む0001など)と後半3文字に分割して処理したいケースを想定。
※文字を切り出して少し離れたセルに出力してますが、非表示にすれば良いかと

Sub moji_trans()
Dim i As Long
Dim v_str As String

With Sheets("sheet1")
For i = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
後半3文字を削除して前半4文字だけ切り出し
.Cells(i, 10) = Replace(.Cells(i, 1), Right(.Cells(i, 1), 3), "")
後半3文字だけ切り出し
.Cells(i, 11) = Right(.Cells(i, 1), 3)

セルの書式を文字列に指定
.Cells(i, 4).NumberFormatLocal = "@"
.Cells(i, 5).NumberFormatLocal = "@"

先頭に0をつける処理
.Cells(i, 4) = WorksheetFunction.Text(.Cells(i, 10), "0000")
.Cells(i, 5) = WorksheetFunction.Text(.Cells(i, 11), "000")
Next
End With
End Sub

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?