0
0

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.

VBAでセルの先頭に文字列挿入

Last updated at Posted at 2019-08-19

#はじめに
初めまして。駆け出しエンジニアです。
備忘録も兼ねて投稿していきます。
今回はVBAの簡単なコードを書いたので載せていきます。

#コードの内容
Excelのシート内のデータで日付、数値になっているセルの先頭にクォーテーション(')を入れるVBAです。
セルのデータを文字列として認識させたいときに便利です。

#ソースコード

'変数宣言
Dim head As String
Dim target As Range


'エスケープを入れるレンジの指定
Selection.SpecialCells(xlCellTypeConstants, 1).Select
head = "'"

'数値もしくは日付の場合エスケープする
For Each target In Selection
    If IsDate(target) Or IsNumeric(target) Then
        target.Value = head & target.Value
    End If
Next
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?