2016-10-19 5 views
0
Dim sht As Worksheet 
    Dim fnd As Variant 
    Dim rplc As Variant 

    fnd = "April" 
    rplc = "May" 

    For Each sht In ActiveWorkbook.Worksheets 
      sht.Cells.Replace what:=fnd, Replacement:=rplc, _ 
        LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _ 
        SearchFormat:=False, ReplaceFormat:=False 
    Next sht 

End Sub 

, 나는 발견하고 내가이 작업을 수행하는 코드를 편집하는 방법 행 (30) entireworkbook에 걸쳐 만에 교체 할 필요 특정 행에 전체 통합 문서를 통해 교체?찾기 & I는 다음과 코드가

+3

'sht.Cells.Replace' 대신'sht.Rows (30) .Replace'를 사용하십시오. – tigeravatar

답변

1

두 가지.

먼저 fnd 및 rplc에 변형을 사용하지 마십시오. 문자열을 사용하십시오.

둘째, "셀"을 사용하는 대신 교체 할 범위를 지정하십시오.

Sub Replacer() 
    Const csFnd As String = "April" 
    Const csRpl As String = "May" 
    Const csRow As String = "A30" 

    Dim sht As Worksheet 

    For Each sht In ActiveWorkbook.Worksheets 
     sht.Range(csRow).EntireRow.Replace _ 
      what:=csFnd, _ 
      Replacement:=csRpl 
    Next sht 
End Sub 

이 아니라 코드의 본문에 variable = "String"을 넣는 것보다, 변하지 않는 텍스트를 개최 코드의 상단에있는 상수를 사용하는 것이 좋습니다이기도합니다. 맨 위에있는 상수로 유지하는 것이 더 쉽습니다.