LoginSignup
0
1

More than 5 years have passed since last update.

[VBA] A,C列を、別シートのA,B列にコピー

Last updated at Posted at 2017-08-06

特定の列(複数)をコピーしたいときのVBAメモ。
列ごとに別の処理を行うとき用のベース。

Sub colCopySample()

    Dim t As Single
    Dim cpY As Long
    Dim psY As Long
    Dim x   As Long
    Dim cpVal As String
    Dim cpWS As Worksheet
    Dim psWS As Worksheet
    Set cpWS = Worksheets("Sheet1") 'コピー元
    Set psWS = Worksheets("Sheet2") 'ペースト先

    Dim cpArr             As Variant
    Dim psArr(1048576, 2) As String
    Dim col(2) As Long

    'コピーする列の配列
    col(0) = Range("A:A").Column
    col(1) = Range("C:C").Column    'MAX_Col

    'コピー範囲 / Cells(min_Row, min_Col) ~ Cells(MAX_Row, MAX_Col)
    Set cpArr = cpWS.Range(cpWS.Cells(1, "A"), _
                           cpWS.Cells(1048576, col(1)))
    cpArr = cpArr.Value

    t = Timer

    For psY = 0 To 1048575

        cpY = psY + 1

        x = 0
        cpVal = cpArr(cpY, col(x))
        If Len(cpVal) <> 0 Then
            psArr(psY, x) = cpVal
        End If

        x = 1
        cpVal = cpArr(cpY, col(x))
        If Len(cpVal) <> 0 Then
            psArr(psY, x) = cpVal
        End If

    Next

    Set cpArr = Nothing

    'ペースト
    psWS.Range(psWS.Cells(1, "A"), psWS.Cells(1048576, "B")).Clear
    psWS.Range(psWS.Cells(1, "A"), psWS.Cells(1048576, "B")) = psArr

    MsgBox "finish! (" & Round(Timer - t, 2) & "秒)"

End Sub
0
1
2

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
1