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