2016-04-08 11 views
0

ブック内の特定のシート(要約)を更新するマクロを作成しています。行を挿入して値を追加する必要がありますが、マクロの実行時にどのワークシートがアクティブであっても、要約シートを常に更新する必要があります。現在は、どのシートがアクティブであっても動作します。私は、範囲またはセル参照を与えるたびに、シートに明示的に名前を付けることを試みました。 (私はマクロにかなり新しいので、マクロを記録して出発点とし、選択とコピーの方法が長らく残っています)。Excel VBAは常に参照シートの代わりに可視シート上で実行されます

Sub PasteValuesSummary() 

    Dim LookupValue As String 

    Dim LookupRange As Range 
    Dim ws As Worksheet 

    Set ws = ThisWorkbook.Sheets("Summary") 

    Set LookupRange = Sheets("Summary").Range("B1:B1000") 

    LookupValue = Sheets("LastUpdate").Range("A1").Value 

    If Not IsError(Application.Match(LookupValue, LookupRange, 0)) Then 

     Sheets("Summary").Select 
     Sheets("Summary").Rows("4:4").Select 
     Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove 
     Sheets("Summary").Range("B4").Select 
     Sheets("Summary").Range("B4") = "=LastUpdate!R[-3]C[-1]" 
     Sheets("Summary").Range("C2:AP2").Select 
     Selection.Copy 
     ActiveWindow.ScrollColumn = 1 
     Sheets("Summary").Range("C4").Select 
     Sheets("Summary").Paste 
     Application.CutCopyMode = False 
     Selection.Copy 
     Sheets("Summary").Range("B4:AP4").Select 
     Application.CutCopyMode = False 
     Selection.Copy 
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 

    End If 

End Sub 
+0

あなたの 'not is noterror ..'行の前に 'ws.Activate'してみてください。 –

+0

これは、アクティブシートを変更していないようで、マクロを実行したときに表示されていたシートに新しい行を挿入する前とまったく同じ動作をします。 –

+0

'xlSheet =ワークシート("要約 ")を設定してから、' Sheets( "Summary") 'を' xlSheet'に変更します。 – gofr1

答えて

2

このコードを試してください。私はそれを取るLookupValue日付にする必要がありますか?

Sub PasteValuesSummary() 

    Dim LookupValue As Date 
    Dim rFound As Range 

    Dim LookupRange As Range 
    Dim ws As Worksheet 

    Set ws = ThisWorkbook.Worksheets("Summary") 

    Set LookupRange = ws.Range("B1:B1000") 

    LookupValue = ThisWorkbook.Worksheets("LastUpdate").Range("A1").Value 

    Set rFound = LookupRange.Find(What:=LookupValue, SearchFormat:=True) 
    If Not rFound Is Nothing Then 
     With ws 'Search help on 'With' keyword. 
      .Rows("4:4").Insert shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove 
      .Range("B4").FormulaR1C1 = "=LastUpdate!R[-3]C[-1]" 
      .Range("C2:AP2").Copy Destination:=.Range("C4") 

      'This is just removing the formula and leaving the values. 
      .Range("B4:AP4").Copy 
      .Range("B4:AP4").PasteSpecial Paste:=xlPasteValues 'No need to add the other arguments - they're default. 
     End With 
    End If 

End Sub 
+1

私は3つの改良点を提案したいと思います:1) 'LookIn'、' LookAt'、 'SearchOrder'から' Set rFound = LookupRange.Find(What:= LookupValue、LookIn:= xlValues、LookAt:= xlWhole、SearchFormat:= True) 'パラメータは、UIからの前の' Find'メソッドの使用時から常に保持されます!常に明示的に設定する方が安全です。 2) '.Rows(" 4:4 ")'の代わりに '.Rows(4)'を使い、読みやすくするために3) 'Range(" B4 ")を使う。value = LookupValue'そして' .Range( "C2 :AP2 ").Copy'と最後に' .Range( "C4").PasteSpecial Paste:= xlPasteValues'を残りの 'With'ステートメントとして:それらは冗長コピー/ペーストなしで同じことを行います – user3598756

関連する問題