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?

mm vbaの基本 列のコピー ペースト 挿入

Posted at
  • vbaで任意の列を複数列に張り付けたい場合(書式ごと)
qiita.bas
Sub CopyAndPasteManyColumns()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") ' "Sheet1"を操作対象のシート名に変更してください

    Dim sourceColumn As String
    sourceColumn = "B" ' コピー元の列(例: "A"列)

    Dim startColumn As Integer
    startColumn = 2 ' 開始列のインデックス(B列は2)

    Dim numColumns As Integer
    numColumns = 10 ' コピーする列数

    Dim i As Integer
    For i = startColumn To startColumn + numColumns - 1
        ws.Columns(sourceColumn & ":" & sourceColumn).Copy
        ws.Columns(i).PasteSpecial Paste:=xlPasteAll
    Next i

    Application.CutCopyMode = False
End Sub

  • vbaで任意の列を複数列に挿入したい場合
qiita.bas
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet2") ' "Sheet2"を操作対象のシート名に変更してください

    Dim sourceColumn As String
    sourceColumn = "B" ' コピー元の列(例: "A"列)

    Dim startColumn As Integer
    startColumn = 3 ' 開始列のインデックス(B列は2)

    Dim numColumns As Integer
    numColumns = 10 ' 挿入する列数

    Dim i As Integer
    For i = startColumn To startColumn + numColumns - 1
        ws.Columns(sourceColumn & ":" & sourceColumn).Copy
        ws.Columns(i).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Next i
    'クリップボートのクリア
    Application.CutCopyMode = False
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?