2017-10-19 1 views
0

그래서 여기에 내 상황이 있습니다. 더 나은 이해를 위해 그림을보십시오. Example tableIF 조건 및 헤더로 값 채우기

그래서 난 할 노력하고있어 columnn C에서 셀이 비어 있지 않은 경우 는, 엑셀 내가 그렇게 매크로 다음 사용 D.을 columnn하는 열 B의 처음 3 편지를 채울 것이다 :

Sub FillCountryCode() 

Sheets("Sheet1").Range("D2:D20").Formula = "=IF(C2 <> """", LEFT(B2,3), """")" 

End Sub 

그래서이 솔루션에는 2 가지 문제가 있습니다.

1st) 국가 코드 열을 이동하면 열이 이동되었다는 사실을 알지 못하므로 매크로가 더 이상 작동하지 않습니다. 그래서 매크로가 작동하도록 매크로를 변경하여 헤더 이름 (예 : 국가 코드)에 따라 올바른 열을 검색 한 다음 모든 행을 통과합니다 (실제 엑셀 파일에는 수백 개의 행이 있습니다. 예제 테이블에만 8이있는 경우). 따라서 매크로의 관련 헤더와 셀이 위치한 열은 실제로 중요하지 않습니다.

2nd) 현재 수동으로 매크로 범위를 결정할 때 매크로에서 파일의 모든 행을 확인하는 올바른 명령은 무엇입니까? 이 문제를 해결하려고 할 때,

여기에 내가 와서 솔루션입니다 (모든 행이 자동차 브랜드나라의 값을 가지고).

Sub FillCountryCode() 


Worksheets("Sheet1").Activate 
Rows(3).Select 

Set CountRY = Selection.Find(What:="COUNTRY", After:=ActiveCell, LookIn:=xlFormulas, _ 
          LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, _ 
          MatchCase:=False, SearchFormat:=False) 

Set ENGINE = Selection.Find(What:="ENGINE", After:=ActiveCell, LookIn:=xlFormulas, _ 
          LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, _ 
          MatchCase:=False, SearchFormat:=False) 

Set COUNTRYCODE = Selection.Find(What:="COUNTRYCODE", After:=ActiveCell, LookIn:=xlFormulas, _ 
          LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, _ 
          MatchCase:=False, SearchFormat:=False) 

Dim LastItemRow As Long 

LastItemRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row 

For pointer = 4 To LastItemRow 

    If Cells(pointer, ENGINE.Column) <> "" Then 

     Cells(pointer, COUNTRYCODE.Column).Value = Sheets("Sheet1").Left(Cells(pointer, COUNTRY.Column), 3) 

    End If 

Next pointer 

End Sub 

이 솔루션을 실행하려고하면 IF 조건에 몇 가지 문제가 있지만 무엇인지 이해할 수 없습니다. 어떤 사람이 나를 도울 수 있습니까? 오류가 발생합니다 : 오류 438 : 개체가이 속성 또는 메서드를 지원하지 않습니다.

+0

1) Application.Match 또는 Find 메서드를 사용하여 올바른 열을 찾고 코드에서 참조 할 수 있습니다. 2) 마지막 행/열을 찾는 방법을 보여주는 수많은 예제가 온라인에 있습니다. 정말로 시도했는데 아무것도 적용 할 수 없었습니까? – SJR

+0

테이블로 서식을 지정하고 테이블 본질 속성을 사용하는 것이 가장 좋지 않습니까? –

+1

안녕하세요. @SJR. 나는 현재의 솔루션을 붙여 넣었다. – Bradi

답변

0

대부분의 작업을 수행 했으므로 점수를받을 자격이 있다고 생각합니다. 약간 수정 된 코드를 게시하여 코드를 약간 더 효율적으로 만들고 오류 메시지가 나타나지 않게하십시오. 변경 사항을 설명하기 위해 몇 가지 설명을 추가했습니다.

Sub FillCountryCode() 

Dim COUNTRY As Range, ENGINE As Range, COUNTRYCODE As Range 'declared all your variables 
Dim LastItemRow As Long, pointer As Long 

With Worksheets("Sheet1") 'removes Selects and need to repeat sheet reference elsewhere 
    Set COUNTRY = .Rows(3).Find(What:="COUNTRY", LookIn:=xlFormulas, _ 
          LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, _ 
          MatchCase:=False, SearchFormat:=False) 'remove ActiveCell reference as will error if not in search range 
    Set ENGINE = .Rows(3).Find(What:="ENGINE", LookIn:=xlFormulas, _ 
          LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, _ 
          MatchCase:=False, SearchFormat:=False) 
    Set COUNTRYCODE = .Rows(3).Find(What:="COUNTRYCODE", LookIn:=xlFormulas, _ 
          LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, _ 
          MatchCase:=False, SearchFormat:=False) 

    If Not COUNTRY Is Nothing And Not COUNTRYCODE Is Nothing And Not ENGINE Is Nothing Then 'avoids error if text not found 
     LastItemRow = .Cells(Rows.Count, "A").End(xlUp).Row 
     For pointer = 4 To LastItemRow 
      If .Cells(pointer, ENGINE.Column) <> "" Then 
       .Cells(pointer, COUNTRYCODE.Column).Value = Left(.Cells(pointer, COUNTRY.Column), 3) 
      End If 
     Next pointer 
    End If 
End With 

End Sub 
+0

답변과 의견을 보내 주셔서 대단히 감사합니다. 나도 똑같은 말을 반복해서 피할 수 있다는 것을 몰랐다. :) – Bradi