2016-06-27 1 views
0

Excel에서 단어를 찾고 해당 셀을 강조 표시하고 싶습니다. VBA를 사용하여 수행하는 방법. 내 코드가 전체 시트를 강조 표시하고 있습니다.나는 한 단어를 찾아 Excel에서 강조하고 싶습니다. 어떻게해야합니까?

은 코드 - 여기

Sub Foreign_Lang_Converter() 

Sheets("Sheet2").Select 
Value = 0 
i = 1 
Do While (Cells(i, 2) <> "") 
Value = Value + 1 
i = i + 1 
Loop 
Count = 0 
For j = 1 To Value 

a = Cells(j, 1) 
b = Cells(j, 2) 
Sheets("Sheet1").Select 
Cells.Select 
    Selection.Find What:=a 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .ThemeColor = xlThemeColorAccent6 
     .TintAndShade = 0.599993896298105 
     .PatternTintAndShade = 0 
    End With 
Selection.Replace What:=a, Replacement:=b 
Sheets("Sheet2").Select 
Next j 
End Sub 
+0

, 그렇지 않으면 우리는에서 오류를 발견하기가 까다로운 찾을거야, 당신이 시도 코드를 게시하여 코드 ... – Dave

+0

안녕 데이브, 내가 질문을 편집하고 내 코드를 추가했습니다. 보세요. !! – snigdha

답변

1

먼저 read this이다.

편집 : 이것은 전체 작업에 대한 해결책은 아니지만 원래 코드로 설명한 문제에 대한 해결책이므로 전체 시트를 채울 것입니다.

여기서 문제는 Selection.Find What:=a이 선택을 변경하지 않으며 범위를 반환한다는 것입니다. 전체 시트가 선택 되었기 때문에 다음 단계는 전체 시트에 색상을 지정합니다. 시도해보십시오.

With Sheets("Sheet1").Cells.Find(a) 
    With .Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .ThemeColor = xlThemeColorAccent6 
     .TintAndShade = 0.599993896298105 
     .PatternTintAndShade = 0 
    End With 
    .Value = b 
End With 

이것은 한 번만 발생합니다. .FindNext 방법이나 조건부 서식을 살펴보십시오. 또한 저장하기 때문에 다른 검색 매개 변수 (LookIn, LookAt, SearchOrderMatchByte)를 설정하는 것이 좋습니다. (비고 here 참조)

편집 : 고정 코드. 지금 일해야한다.

+0

안녕 Dave, 이 코드는 오류를 보여줍니다. .Interior 및 .Value = b에 오류가 있습니다. 원하는 출력이 얻어지지 않습니다. 즉, sheet1의 대체 항목을 강조 표시하지 않습니다. – snigdha

+0

@snigdha Dave가 아니지만 절대로 잊지 마십시오.). 항상 어떤 종류의 오류가 나타나는지 지정하십시오. 이 코드를 워크 시트에서 실행할 때, 'a'의 * occurence를'b '로 대체하고 해당 셀의 색상을 설정합니다. – arcadeprecinct

0

나는 해상도를 가지고

코드는 것 같은 - 첫째

Sub Foreign_Lang_Converter() 

Sheets("Sheet2").Select 
Value = 0 
i = 1 
Do While (Cells(i, 2) <> "") 
Value = Value + 1`enter code here` 
i = i + 1 
Loop 
Count = 0 
For j = 1 To Value 

a = Cells(j, 1) 
b = Cells(j, 2) 
Sheets("Sheet1").Select 


    Cells.Select 
    Application.ReplaceFormat.Clear 
    With Application.ReplaceFormat.Font 
     .Subscript = False 
     .TintAndShade = 0 
    End With 
    With Application.ReplaceFormat.Interior 
     .PatternColorIndex = xlAutomatic 
     .Color = 5296274 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
    Selection.Replace What:=a, Replacement:=b, LookAt:=xlPart, _ 
     SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ 
     ReplaceFormat:=True 

Sheets("Sheet2").Select 
Next j 
End Sub 
+0

이것은 'Range.Find' 메소드보다 나은 해결책입니다. 그러나 선택 항목을 사용해서는 안됩니다. 자세한 내용은 [this] (http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)를 참조하십시오. – arcadeprecinct