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 1 year has passed since last update.

セル範囲を配列に格納してコピーペーストする

Copy メソッドを使えばできるので、配列にする必要はありませんが、配列の練習として書きました。

Dim arr() As Variant
  
ReDim arr(Range("A4").CurrentRegion.Count - 1) As Variant

変数arr を Variant型で宣言
ReDim で配列変数 arrの要素数を動的に割り当て(格納したい値が入っているセル範囲をCurrentRegionで取得し、Count - 1 で最終行を取得、配列の要素は0から始まるため - 1 で部屋の数(要素数)を調整、- 1 がないと1多くなる)

Dim i As Long
Dim j As Long
  
  For Each x In Range("A4").CurrentRegion
  
    arr(i) = x.Value
    
    i = i + 1
    
  Next x

セル範囲 Range("A4").CurrentRegion を For Eachで配列変数arr に1つずつ格納

  For j = LBound(arr) To UBound(arr)
  
    Cells(j + 4, 2) = arr(j)
    
  Next j

arrの中身をFor next で隣の列にコピー

Sub CopyByArray()

  Dim arr() As Variant
  
  ReDim arr(Range("A4").CurrentRegion.Count - 1) As Variant
  
  Dim i As Long
  Dim j As Long
  
  For Each x In Range("A4").CurrentRegion
  
    arr(i) = x.Value
    
    i = i + 1
    
  Next x
  
  For j = LBound(arr) To UBound(arr)
  
    Cells(j + 4, 2) = arr(j)
    
  Next j
  
End Sub

画像2.png

A列4行目から値が入っていれば、何行目まで入っていても同じコードですべてコピーできます。

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?