2在同一个宏中的循环

问题描述:

我一直在处理这个代码,它有一个循环来打开列中的每个文件,然后应该做一个复制列的每一行的循环。当我运行第一个循环它工作正常,但是当我添加第二个它不工作。有人能帮助我吗?非常感谢您2在同一个宏中的循环

Sub teste() 

Dim myRng As Range 

Dim myCell As Range 

Workbooks("TIME_SHEET_MACRO").Activate 

With WorkSheets("Base de dados") 

    Set myRng = .Range("E1", .Cells(.Rows.Count, "E").End(xlUp)) 

    Set myRnge = .Range("B2", .Cells(.Rows.Count, "B").End(xlUp)) 

    For Each myCell In myRng.Cells 

     For Each myCells In myRnge.Cells 

     If .Cells(myCell.Row, "E").Value <> "" Then 

      Workbooks.Open (myCell) 

      Workbooks("TIME_SHEET_MACRO").Activate 

      WorkSheets("Base de dados").Activate 

     If .Cells(myCells.Row, "B").Value <> "" Then 

      ActiveCell.Copy 

     End If 

    Next myCell 

End With 

End Sub 
+2

总是帮助格式化你的代码,并使用缩进去帮助别人读它... –

+2

你错过了'next'和'结束如果' – findwindow

For Each myCell In myRng.Cells 
    For Each myCells In myRnge.Cells 
     If .Cells(myCell.Row, "E").Value <> "" Then 
      Workbooks.Open (myCell) 
      Workbooks("TIME_SHEET_MACRO").Activate 
      WorkSheets("Base de dados").Activate 
     If .Cells(myCells.Row, "B").Value <> "" Then 
      ActiveCell.Copy 
     End If 
Next myCell 

应该是─

For Each myCell In myRng.Cells 
    For Each myCells In myRnge.Cells 
     If .Cells(myCell.Row, "E").Value <> "" Then 
      Workbooks.Open (myCell) 
      Workbooks("TIME_SHEET_MACRO").Activate 
      WorkSheets("Base de dados").Activate 
     End If 
     If .Cells(myCells.Row, "B").Value <> "" Then 
      ActiveCell.Copy 
     End If 
    Next myCells 
Next myCell