2016-06-14 5 views
0

JNevillは非常に親切にクリーンアップしてマクロを改善しましたが、残念ながらエラーが発生して元に戻ってしまいました役職。うまくいけば、誰かが彼が中断したところで拾うことができます。Excel VBA - 範囲に応じて特定のシートにコピーするコードでエラーが発生しました

私は取得しています障害がある - オブジェクト変数またはWithブロック変数がそれを設定していないlastCell = Range("A" & Rows.Count).End(xlUp).Offset(1)

Sub Macro1() 
'Make a variable to store the cell found 
Dim lastCell as Range 

'find the last cell in Column A of the active sheet 
lastCell = Range("A" & Rows.Count).End(xlUp).Offset(1) 

'Paste in the I16 value 
lastCell.value = RangE("I16").value 

'Grab whatever is hanging out in Column B next to the last cell and stick it in J20 
Range("J20").value = lastCell.Offset(0,1).value 

'Test to see if I16 has value "R" 
If Range("I16").value = "R" Then 

    'Find the last row in Sheet7, Column B and store it to the variable 
    lastCell = Range("B" & Rows.Count).End(xlUp).Offset(1) 

    'Copy J20 value to the lastCell in Sheet 7, Column B 
    lastCell = Range("J20").value 
End if 


End Sub 

オリジナルのポストはここにORIGINAL

答えて

0

あるラインに反していることがありますので、あなただけの(Setを逃していますオブジェクト変数):

Set lastCell = Range("A" & Rows.Count).End(xlUp).Offset(1) 

以下のコメントに基づいて、私はこれがあなたの望むようなものだと考えています:

Sub Macro1() 
    Dim ws     As Worksheet 
    'Make a variable to store the cell found 
    Dim lastCell    As Range 

    'find the last cell in Column A of the active sheet 
    Set lastCell = Range("A" & Rows.Count).End(xlUp).Offset(1) 

    'Paste in the I16 value 
    lastCell.Value = Range("I16").Value 

    'Grab whatever is hanging out in Column B next to the last cell and stick it in J20 
    Range("J20").Value = lastCell.Offset(0, 1).Value 

    'Test to see if I16 has value "R" 
    Select Case VBA.UCase$(Range("I16").Value2) 
     Case "R" 
      Set ws = sheet7 
     Case "C" 
      Set ws = sheet3 
     Case "P" 
      Set ws = Sheet1 
     Case "S" 
      Set ws = Sheet2 
    End Select 

    If Not ws Is Nothing Then 
     'Find the last row in Sheet7, Column B and store it to the variable 
     Set lastCell = ws.Range("B" & Rows.Count).End(xlUp).Offset(1) 

     'Copy J20 value to the lastCell in Sheet 7, Column B 
     lastCell = Range("J20").Value 
     Application.Goto lastCell, True 
    End If 

End Sub 
+0

おかげロリーは、そのが障害を停止していますが、気にしないならば、それはまだ、またsheet7 –

+0

にそれを貼り付けていない、それが {Cを使用している文字に依存しに行く可能性が数枚を持っています} = sheet3 {P} = sheet1 {S} = sheet2 これもバインドに入れることができれば、idは最も感謝します –

+0

このコードのどこかでSheet7を参照していないので、すべての「Range」呼び出しは、アクティブシート(コードが通常のモジュールにある場合)またはコードを含むワークシートのいずれかにデフォルト設定されます。 – Rory

関連する問題