LoginSignup
0
2

More than 1 year has passed since last update.

【ExcelVBA】文字列を改行コードごとに分割して下のセルに貼り付ける

Last updated at Posted at 2022-01-22

はじめに

個人的に実施している作業の中で、「文章をセルに貼り付け、それを改行ごとに分割して下のセルに貼り付ける 」という作業があったのでマクロを組んでみました。
特定の行・列に対する操作ではないので、アクティブセルに対して作業を行うマクロとなります。

作業内容

1.アクティブセルを取得
2.改行コードごとに文字列を分割
3.分割した文字列を下のセルに貼り付け(ループ)

コード

ExcelVBA
Option Explicit
Sub splitSentencesByLine()
    'アクティブなセルを取得
    Dim sentence As Variant

    sentence = ActiveCell.Value

    '改行コードごとに文字列を分割
    Dim tmp As Variant
    tmp = Split(sentence , vbLf)

    Dim i, j As Integer
    Dim deleteCharPlace As Long

    j = 0

    For i = 0 To UBound(tmp)
        '下のセルに貼り付け
            Cells(ActiveCell.Row + j, ActiveCell.Column) = tmp(i)
            j = j + 1
    Next

End Sub

動作

・マクロ動作前
image.png

・マクロ動作後
image.png

参考にしたサイト

VBAで文字列を改行コードで分割する
VBA セルの位置を取得する

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