0
1

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.

AccessでコピーしたデータをExcelに貼り付けるVBA

Posted at

AccessでコピーしたデータをExcelに貼り付ける際、思い通りに貼り付けられないことが多々ある。
これはクリップボード内のAccessデータを文字列形式でExcelに貼り付けるためのスクリプト。
パフォーマンスがあまり良くないところが問題。

Option Explicit

Sub AccessTablePaste()
    Application.ScreenUpdating = False
    
    Dim DObj As New DataObject
    DObj.GetFromClipboard
    
    Dim curRow As Long, curCol As Long
    curRow = ActiveCell.Row
    curCol = ActiveCell.Column
    
    Dim rows() As String
    Dim fields() As String
    Dim rIdx As Long, cIdx As Long
    
    rows = Split(DObj.GetText, vbCrLf)
    For rIdx = 0 To UBound(rows)
    
        fields = Split(rows(rIdx), vbTab)
        For cIdx = 0 To UBound(fields)
            
            With Cells(curRow + rIdx, curCol + cIdx)
                .NumberFormatLocal = "@"
                .value = fields(cIdx)
            End With
            
        Next
    Next
    
    Application.ScreenUpdating = True
End Sub

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?