これはあなたがしようとしていることですか?コードを理解するのに問題がないように、コードにコメントを付けました。
ロジック:
- データをフィルタリングする
- 一時シートを作成する
- フィルター処理されたデータを一時シートにコピー
- 不要な列を削除 (A,B,D,E,G,J,K,L)
- 関連する列 (C、F、H、I) を C、I、H、および F に再配置します。
- 最後に一時シートを削除します (IMP: コードの最後にあるメモを読んでください)
コード (試行錯誤)
Option Explicit
Sub Sample()
Dim ws As Worksheet, wsTemp As Worksheet
Dim rRange As Range, rngToCopy As Range
Dim lRow As Long
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Get the Last Row
lRow = .Range("L" & .Rows.Count).End(xlUp).Row
'~~> Set your range for autofilter
Set rRange = .Range("A5:L" & lRow)
'~~> Remove any filters
.AutoFilterMode = False
'~~> Filter, copy visible rows to temp sheet
With rRange
.AutoFilter Field:=12, Criteria1:="Example"
'~~> This is required to get the visible range
ws.Rows("1:4").EntireRow.Hidden = True
Set rngToCopy = .SpecialCells(xlCellTypeVisible).EntireRow
Set wsTemp = Sheets.Add
rngToCopy.Copy wsTemp.Range("A1")
'~~> Unhide the rows
ws.Rows("1:4").EntireRow.Hidden = False
End With
'~~> Remove any filters
.AutoFilterMode = False
End With
'~~> Re arrange columns in Temp sheet so that we get C, I, H and F
With wsTemp
.Range("A:B,D:E,G:G,J:L").Delete Shift:=xlToLeft
.Columns("D:D").Cut
.Columns("B:B").Insert Shift:=xlToRight
.Columns("D:D").Cut
.Columns("C:C").Insert Shift:=xlToRight
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set rngToCopy = .Range("A1:D" & lRow)
Debug.Print rngToCopy.Address
'~~> Copy the range to clipboard
rngToCopy.Copy
End With
'NOTE
'
'~~> Once you have copied the range to clipboard, do the necessary
'~~> actions and then delete the temp sheet. Do not delete the
'~~> sheet before that. An alternative would be to use the APIs
'~~> to place the range in the clipboard so you can safely delete
'~~> the sheet before performing any actions. This will not clear
'~~> clear the range if the sheet is immediately deleted.
'
'
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
スクリーンショット
コード実行前の Sheet1

フィルター処理されたデータを含む一時シート

ファローアップ
境界線を削除するには、このコードを上記のコードに追加できます
With rngToCopy
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
.Borders(xlEdgeTop).LineStyle = xlNone
.Borders(xlEdgeBottom).LineStyle = xlNone
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
end with
上記のコードを行の後に入れますDebug.Print rngToCopy.Address