2014-10-16 2 views
0

필자는 muuliple 시트가 있고 한 시트에 "A2"를 복사하고 다른 시트에 값 (또는 셀에 포함 된 수식)을 붙여 넣기를 원합니다. 이 코드를 사용하지만 VBA는 "cell.Select"행을 노란색으로 강조 표시합니다. 해결 방법, 알고 계십니까?VBA, 도서의 모든 시트에 유효한 범위의 이름을 설정하는 방법?

Sub test() 

Set cell = Range("a2") 

cell.Select 
Selection.Copy 

Sheets("one").Activate 
cell.Select 
ActiveSheet.Paste 

Sheets("two").Activate 
cell.Select 
ActiveSheet.Paste 

End Sub 

감사 니콜라이

답변

0

주어진 범위의 셀을 설정하면, 당신이 그것을 선택할 필요가 없습니다. 대신 그것을 참조 할 수 있습니다. 당신은 또한 설정하면 워크 시트는 결국에 붙여 넣기 할 객체 위의 다음 선택하지 않고, 더 응축하고 직접적인 방식으로 기록 될 수있다 :

Sub test() 

    Set cell = Range("A2") 'implicit reference to activesheet in activeworkbook 

    Set ws1 = Sheets("one") 'requires that you have a sheet named "one" in the activeworkbook 
    Set ws2 = Sheets("two") 'requires that you have a sheet named "two" in the activeworkbook 

    'assign the value to somewhere in the ws1 and ws2 
    ws1.Range("A2") = cell 
    ws2.Range("A2") = cell 

End Sub 

편집 : 복사본을 사용

버전에 수식 유지

Sub test() 

    Set cell = Range("A2") 'implicit reference to activesheet in activeworkbook 

    Set ws1 = Sheets("one") 'requires that you have a sheet named "one" in the activeworkbook 
    Set ws2 = Sheets("two") 'requires that you have a sheet named "two" in the activeworkbook 

    'assign the value to somewhere in the ws1 and ws2 
    cell.Copy Destination:=ws1.Range("A2") 
    cell.Copy Destination:=ws2.Range("A2") 


End Sub 
0
Sub test() 
    Dim cl As String 
    cl = Range("a2").Address 
    Range(cl).Copy Destination:=Sheets("one").Range(cl) 
    Range(cl).Copy Destination:=Sheets("two").Range(cl) 
End Sub 
관련 문제