2014-06-16 2 views
0

특정 셀에서 텍스트의 일부를 추출하려면 수식을 입력해야합니다. 이것은 알려지지 않은 횟수만큼 반복해야합니다. find 함수를 사용하여 do 문을 사용하고 있습니다. 불행히도 첫 번째 발생과 루프를 계속해서 찾아냅니다. 다음 가치를 찾아내는 방법이 있습니까? 아무도 도와 줄 수 있습니까?사용 Excel VBA에서 특정 텍스트를 찾을 때까지 수행

Do 

    With Columns("B") 
     .Find(What:="total", After:=.Cells(1, 1), LookIn:=xlValues).Activate 
    End With 

    ActiveCell.Offset(0, -1).Select 
    ActiveCell.FormulaR1C1 = "=LEFT(RC[1],12)" 
    ActiveCell.Offset(1, 0).Activate 
    ActiveCell.FormulaR1C1 = "=LEFT(RC[1],12)" 



    Loop Until ActiveCell.Offset(0, 1) = "Grand Total" 


    End Sub 

Thank you for your assistance. 
jfabro 

I have been able to make the macro work to loop through the entire workseet however it does not stop when the condition is met. I believe the problem lies in the condition. What I am needing to say is to stop if the cell to the right of the active cell contains Grand Total. 

Here is the new code I am using: 
Do Until ActiveCell.Offset(0, 1) = "*Grand Total*" 

With Columns("B") 
    Cells.Find(What:="total", After:=ActiveCell, LookIn:=xlValues, LookAt:= _ 
     xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ 
     , SearchFormat:=False).Activate 
End With 

ActiveCell.Offset(0, -1).Select 
ActiveCell.FormulaR1C1 = "=LEFT(RC[1],12)" 
Cells.FindNext(After:=ActiveCell).Activate 

If ActiveCell.Offset(0, 1) = "*Grand Total*" Then 


Exit Do 

End If 

Loop 





End Sub 
+0

그것을 여기

은 내가 사용하고 코드입니다 루프를 실행할 때마다 증가하는 인덱스 변수를 사용하기 위해'.Cells (1,1)'레퍼런스를 변경해야 할 것 같은데. 아마도'.Cells (index, 1)'과 같은 것입니다. (... 루프 안에 ... ...)'index = index + 1' – chancea

+1

http://msdn.microsoft.com/en-us/library/office/ff839746 (v = office.15) .aspx는 루프에서 '찾기'를 사용하는 방법을 보여줍니다. –

+0

그 덕분에 Tim이 이제는 오류가 나타나기 전에 전체 파일을 두 번 실행합니다. – user3689031

답변

0

뭔가가 당신의 문자열 일치 잘못 생각, 내가 바꿀 것 :

If ActiveCell.Offset(0, 1) = "*Grand Total*" Then 


Exit Do 

End If 

과 :

If Instr(1,ActiveCell.Offset(0,1).value,"Grand Total")<>0 then Exit Do 
관련 문제