2013-06-03 7 views
2

프로그래밍에 관심이 있으며 VBA로 시작하기로 결정했습니다. 그래서 코드에 문제가 있습니다. 수식 결과가 TRUE 인 셀을 식별하고 선택한 동일한 행의 첫 번째 셀 내용을 지워야합니다. 그러나 루프를 사용할 때 매크로는 동일한 결과를 3 번 ​​반환합니다 (변경해야하는 정확한 행 수). 나는 아래에 나의 코드를 넣을 것이다. 누군가 도움을 줄 수 있습니까?VBA에서 find 메소드 사용하기

감사합니다 !!!

Sub Teste2sigma() 

Windows("1.xls").Activate 
    Sheets("Standard 1").Activate 
    Range("AI3:AJ42").Select 
    With Range("AI3:AJ42") 
      Set C = .Find("TRUE", LookIn:=xlValues) 
      If Not C Is Nothing Then 
       ClearAddress = C.Address 
       ClearRow = C.Row 
       ClearColumn = C.Column 
       Do 
        Cells(ClearRow, 1).Select 
        Cells(ClearRow, 1).ClearContents 
        ClearRows = ClearAddress & "," & C.Address(RowAbsolute:=False) 
        'Cells(ClearRow, ClearColumn).Select 
        Set C = .FindNext(After:=C) 
       Loop While Not C Is Nothing And C.Address <> ClearAddress 
      End If 
     End With 


End Sub 

답변

3

크레이그 T (Craig T)가 지적한 바와 같이 명확한 행동이 잘못되었습니다. 또한 코드를 간소화했습니다.

Option Explicit 
    Sub Teste2sigma() 

    Dim c As Range 
    Dim ClearAddress As String 
    Dim ClearRow As Long 

    With ThisWorkbook.Worksheets("Sheet1").Range("AI3:AJ42") 
     Set c = .Find(What:=True, LookIn:=xlValues, lookat:=xlPart) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Cells(ClearRow, 1).ClearContents 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 
    End With 

    End Sub 
+0

은 BTW 루프 라인은'C 여기에 아무것도 없을 수 없기 때문에 루프 c.Address <> ClearAddress'은 (이미 테스트 동안 일한다고) 그리고 그 라인의 경우 Address 속성을 테스트하려고 시도하여 오류가 발생했습니다. – JosieP

+0

Thanks @ JosieP. 코드를 수정했습니다. – chuff

2

루프 내에 ClearRow 지정을 시도해보십시오.

Do 
    ClearRow = C.Row 
    Cells(ClearRow, 1).Select 

루프 이전에 ClearRow를 지정하면 매번 동일한 셀의 내용을 지우는 것이됩니다.

0

답변 해 주셔서 감사합니다. 나는 chuff가 쓴 코드를 사용했고 효과가 있었다. 그러나, 이것은 더 큰 코드의 일부일 뿐이며, 많은 주문 woorkbooks (1 - n)에서 7 장을 사용하여 동일한 작업을 수행해야합니다. 그래서, 나는 각 woorkbook에 대한 코드를 반복했지만 아무것도 변경하지 않습니다. 한 가지 중요한 점은 "True"는 Excel에서 OR 함수의 결과입니다.

Sub Teste2Sigma() 


Windows("datacao_v2.xls").Activate 
     Sheets("SamList").Activate 
     Range("f2").Select 
     varPasta = ActiveCell 
     varDatacao = varPasta & "datacao_v2" 
     Sheets("SamList").Select 
     Columns(1).Find(What:="fim").Activate 
     fim = ActiveCell.Row 'linha correspondente ao "fim" 
     first = Cells(4, 3) 
     aux = Range(Cells(fim - 3, 1), Cells(fim - 3 - 9, 1)).Find(What:="GJ", SearchDirection:=xlPrevious).Row 
     last = Cells(aux + 1, 3) 
     nInt = (fim - (fim - aux) - 3)/(first + 2) 
     nVal = first * nInt + last 'número total de amostras lidas 
     nPlan = Ceiling(nVal/4) 
     media = Range(Cells(2, 1), Cells(nPlan + 1, 1)) 


    For K = 1 To nPlan 

     Windows(K & ".xls").Activate 



    Dim c As Range 
    Dim ClearAddress As String 
    Dim ClearRow As Long 

    With Worksheets("Standard 1").Range("AI3:AJ42") 
     Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Cells(ClearRow, 1).Value = "" 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 
    End With 

    With Worksheets("Standard 2").Range("AI3:AJ42") 
     Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Cells(ClearRow, 1).Value = "" 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 
    End With 

With Worksheets("Sample 1").Range("AI3:AJ42") 
     Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Cells(ClearRow, 1).Value = "" 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 
    End With 

With Worksheets("Sample 2").Range("AI3:AJ42") 
     Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Cells(ClearRow, 1).Value = "" 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 
    End With 

With Worksheets("Sample 3").Range("AI3:AJ42") 
     Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Cells(ClearRow, 1).Value = "" 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 
    End With 

With Worksheets("Sample 4").Range("AI3:AJ42") 
     Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Cells(ClearRow, 1).Value = "" 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 
    End With 

     With Worksheets("Blank").Range("Z7:Z46") 
      Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart) 
      If Not c Is Nothing Then 
       ClearAddress = c.Address 
       Do 
        ClearRow = c.Row 
        Cells(ClearRow, 1).Value = "" 
         Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
      End If 
    End With 

     With Worksheets("Blank").Range("AA7:AA46") 
      Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart) 
      If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Cells(ClearRow, 23).Value = "" 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 
    End With 

Next K 


End Sub 
+0

또 다른 차이점은 What : = "True"및 Cells (ClearRow, 1) .ClearContents입니다. 울부 짖음으로 원래 코드 (What : = "True")를 사용하여 지워서는 안되는 셀이있었습니다. 또한 .Clearcontents를 사용하면 병합 된 셀에 문제가 발생했습니다. – Felipe

0

여러분, 지금은 모든 것이 좋습니다 !!! 최종 코드 아래

문제는 다음 "함께"시작하기 전에 C 변수가 깨끗해야한다는 것입니다. 그래서 I've는 다음과 같이 "와"모든 전에 Set c = Nothing를 삽입 :

Set c = Nothing 

    Worksheets("Sample 2").Activate 

With Worksheets("Sample 2").Range("AI3:AJ42") 
     Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Cells(ClearRow, 1).Value = "" 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 
    End With 

    Set c = Nothing 

    Worksheets("Sample 3").Activate 

With Worksheets("Sample 3").Range("AI3:AJ42") 
     Set c = .Find(What:="True", LookIn:=xlValues, lookat:=xlPart) 
     If Not c Is Nothing Then 
      ClearAddress = c.Address 
      Do 
      ClearRow = c.Row 
      Cells(ClearRow, 1).Value = "" 
      Set c = .FindNext(c) 
      Loop While c.Address <> ClearAddress 
     End If 
    End With 
관련 문제