2015-01-21 6 views
0

이라는 .xlsx에서 열 A와 B를 가져와 BS 및 BT 열에 붙여 넣으려고합니다. 6 (BS6 및 BT6). 그것은 항상 작동런타임 오류 1004 - 메서드 'Object'_Global '의'Range '메서드가 실패했습니다.

Workbooks.Open ThisWorkbook.Path & "\..\macro\options.xlsx" 

Workbooks("options.xlsx").Activate 
Set c = .Find("licensePlate", LookIn:=xlValues) 
Range(c.Offset(1, 0), Range(c.Address).End(xlDown)).Copy 
ThisWorkbook.Activate 
Sheets("example").Activate 
Range("BS6").PasteSpecial Paste:=xlPasteValues 

Workbooks("options.xlsx").Activate 
Set c = .Find("description", LookIn:=xlValues) 
Range(c.Offset(1, 0), Range(c.Address).End(xlDown)).Copy 
ThisWorkbook.Activate 
Sheets("example").Activate 
Range("BT6").PasteSpecial Paste:=xlPasteValues 

Workbooks("options.xlsx").Close 

ThisWorkbook.Activate 

(I 정확히이 매크로에서 이와 같은 코드 이상 6-7 부분이 있고, 아무 문제에 없음) : 나는 그것을 할 때, 나는 일반적으로 사용하는 코드입니다 이

나를 도울 수

(Range(c.Offset(1, 0), Range(c.Address).End(xlDown)).Copy)

희망 누군가 라인 (5)에 실패하는 이유하지만 난 모르겠어요.

+0

질문의이 유형이 이전에 요청하고있다. 사이트 또는/및 google을 검색하십시오 –

+1

이미 검색되었지만 정확한 답변을 찾을 수 없으며 vba 전문가가 아닙니다. – smtdev

+1

'c'가 실제로 무엇인가를 발견했는지 확인하십시오.'licensePlate'가 당신이 찾는 곳에서 발견되지 않을 수도 있습니다. 감시자를'c'로 설정하고'Set c = ...'명령문 다음에 디버그 코드를 실행하십시오. –

답변

1

With Range을 사용하지 않으므로 .Find은 범위를 나타내는 것으로 보이지 않습니다. 따라서 cNothing으로 설정되고 OffsetNothing Range으로 설정하면 오류가 발생합니다. , 내가보기 엔 당신이 Activate를 방지하는 것이 좋습니다 것 또한

Set c = Workbooks("options.xlsx").Sheets("name of sheet").Cells.Find("licensePlate", LookIn:=xlValues) 
If c is Nothing Then 
    Msgbox "licensePlate Not Found" 
Else 
    'Run Code 
End If 

:

당신은 당신이 뭔가를 사용할 수있는 시트 전체를 검색하려면

If c is Nothing Then 
    Msgbox "licensePlate Not Found" 
Else 
    'Run Code 
End If 

처럼 에러 체크를 사용해야합니다. 대신 always define the object that you are using a method on을 입력해야합니다.

편집 : 당신이 당신의 시트를 정의하지 않는 Range 중 하나를이 같은

뭔가 작동합니다 :

Dim ws1 as Worksheet, Dim c As Range 
Set ws1 = Workbooks("options.xlsx").Sheets("name of sheet") 
Set c = ws1.Cells.Find("licensePlate", LookIn:=xlValues) 
If c is Nothing Then 
    Msgbox "licensePlate Not Found" 
Else 
    ws1.Range(c.Offset(1, 0), c.End(xlDown)).Copy 
    ThisWorkbook.Sheets("example").Range("BS6").PasteSpecial Paste:=xlPasteValues 
End If 
+0

'c'는 "licensePlate"를 얻는 것 같습니다. 나는 'Else'뒤에 Msgbox를 놓았고 "licensePlate"를 표시하면 실패합니다. – smtdev

+0

Msgbox c.Address는 $ B $ 1을 반환합니다. | ... 나는 코드의 어떤 부분에서 범위를 어둡게하지 않는다! 이 오류로 인해 약간 손실되었습니다. – smtdev

+0

그것은 작동합니다! 그것은 내가 필요한 모든 것을 얻습니다. 고마워요.] – smtdev

관련 문제