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 3 years have passed since last update.

Excel沼が避けられない... ペタペタを減らす

Last updated at Posted at 2018-01-16

##やりたいこと:
####Excelの同じブック内のSheet2からSheet1へ行と列を入れ替えて貼り付ける
形式を選択して貼り付け→値(V)→行/列の入れ替え(E)を自動化
ただただ見栄えのためにペタペタしなければならない方いたら参考にしてください。

無題.png

[形式を選択して貼り付け→値(V)→行/列の入れ替え(E)]をMacro1のサブルーチンにまとめ、exeでセルを指定して呼び出して実行

'x1..コピー元の開始セル
'x2..コピー元の終端セル
'y1..コピー先のセル
Private Sub Macro1(x1 As String, x2 As String, y1 As String)
Dim MB As Workbook
Set MB = ThisWorkbook

MB.Sheets("Sheet2").Select
MB.Sheets("Sheet2").Range(x1 & ":" & x2).Select
Selection.Copy
Sheets("Sheet1").Select
Range(y1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=True
Application.CutCopyMode = False
Range("A1").Select
MB.Sheets("Sheet2").Select
MB.Sheets("Sheet2").Range("A1").Select
End Sub

Sub exe()
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim MB As Workbook
Set MB = ThisWorkbook
MB.Sheets("Sheet1").Select
Range("c2:i10").ClearContents

Call Macro1("B2", "D8", "C2")
Call Macro1("B10", "D16", "C5")
Call Macro1("B18", "D24", "C8")
End Sub

選択するべきセルが一定の間隔で指定可能ならforで繰り返し

Sub exe()
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim MB As Workbook
Set MB = ThisWorkbook

MB.Sheets("Sheet1").Select
Range("c2:i10").ClearContents

Dim b,c,d As Long
b = 2
d = 8
For c = 2 To 8 Step 3
Call Macro1("B" & b, "D" & d, "C" & c)
b = b + 8
d = d + 8
Next c
End Sub
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?