2016-04-04 3 views
1

プロシージャがあまりにも大きいエラーを受信して​​いるところに達しました。これは、私のコードが非常に厄介であるためです。当該セクションは、以下:VBAコードのこの特定のビットを小さくするにはどうすればよいですか?

If patientsperrespondentpertimepoint = 1 Then 
Sheets("Work").Select 
Range("D2:D" & patientprofiles + 1).Select 
Selection.Copy 
Sheets("Output").Select 
Range("B2").Select 
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
    False, Transpose:=True 
ElseIf patientsperrespondentpertimepoint = 2 Then 
Sheets("Work").Select 
Range("D2:D" & patientprofiles + 1).Select 
Selection.Copy 
Sheets("Output").Select 
Range("B2").Select 
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
    False, Transpose:=True 
Sheets("Work").Select 
Range("D" & patientprofiles + 2 & ":D" & 2 * patientprofiles + 1).Select 
Selection.Copy 
Sheets("Output").Select 
Range("B3").Select 
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
    False, Transpose:=True 
ElseIf patientsperrespondentpertimepoint = 3 Then 
Sheets("Work").Select 
Range("D2:D" & patientprofiles + 1).Select 
Selection.Copy 
Sheets("Output").Select 
Range("B2").Select 
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
    False, Transpose:=True 
Sheets("Work").Select 
Range("D" & patientprofiles + 2 & ":D" & 2 * patientprofiles + 1).Select 
Selection.Copy 
Sheets("Output").Select 
Range("B3").Select 
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
    False, Transpose:=True 
Sheets("Work").Select 
Range("D" & 2 * patientprofiles + 2 & ":D" & 3 * patientprofiles + 1).Select 
Selection.Copy 
Sheets("Output").Select 
Range("B4").Select 
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
    False, Transpose:=True 

これは継続し、patientsperrespondentpertimepoint 3 4から5に一つずつ12までのすべての方法を成長し、対応するコピー&ペーストコマンドが各ステップで添加しますはしご。私の質問は、どうすればこれを短縮できますか?多くのコードが繰り返されているので、私はそれを短くしてよりエレガントにブートする方法を見つけることができるのだろうかと思っています。ありがとう!

+1

ビルド機能があり作ることができるいくつかのより多くの最適化がありますが、これはあなたのコードがより簡潔にするもののアイデアを提供しますか?これはコードレビューに適しています。 – findwindow

+6

[Excel VBAマクロでの選択の使用を避ける方法](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)を参照してください。あなたの目標を達成するためにselectとactivateに頼ってください。 – Jeeped

+1

['.Select'を避ける方法を読む](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)、これはあなたをかなり遠くにします。 – BruceWayne

答えて

3
Dim i As Long 
For i = 0 To patientsperrespondentpertimepoint - 1 
    Worksheets("Work").Range("D" & (i * patientprofiles + 2) & ":D" & ((i + 1) * patientprofiles + 1)).Copy 
    Worksheets("Output").Range("B2").Offset(i, 0).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True 
Next 
+1

これは完全に機能します。何百行ものコードを5行に置き換えるのは本当に印象的です。ありがとう! – sarcasm24

+0

良い解決策。ワークシートの参照変数を作成し、セル(Cells)メソッドを使用してRange(...)の代わりにTarget範囲を直接参照することで、パフォーマンスを向上させることができます。 – ThunderFrame

1

これを試してください。 ...

Sub Foo() 

    Dim shtWork As Worksheet 
    Dim shtOut As Worksheet 

    'I've qualified the workbook as ThisWorkbook, but you might want to be more specific if the sheets are in a different workbook 
    Set shtWork = ThisWorkbook.Sheets("Work") 
    Set shtOutput = ThisWorkbook.Sheets("Output") 

    If patientsperrespondentpertimepoint = 1 Then 
    shtWork.Range("D2:D" & patientprofiles + 1).Copy 
    shtOut.Range("B2").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=True 
    ElseIf patientsperrespondentpertimepoint = 2 Then 
    shtWork.Range("D2:D" & patientprofiles + 1).Copy 
    shtOut.Range("B2").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=True 
    shtWork.Range("D" & patientprofiles + 2 & ":D" & 2 * patientprofiles + 1).Copy 
    shtOut.Range("B3").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=True 
    ElseIf patientsperrespondentpertimepoint = 3 Then 
    shtWork.Range("D2:D" & patientprofiles + 1).Copy 
    shtOut.Range("B2").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=True 
    shtWork.Range("D" & patientprofiles + 2 & ":D" & 2 * patientprofiles + 1).Copy 
    shtOut.Range("B3").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=True 
    shtWork.Range("D" & 2 * patientprofiles + 2 & ":D" & 3 * patientprofiles + 1).Copy 
    shtOut.Range("B4").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=True 
    'I've added a closing 'End If here 
    End If 

End Sub 
関連する問題