2在同一个宏中循环

时间:2015-10-26 18:44:47

标签: vba excel-vba excel

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

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

1 个答案:

答案 0 :(得分:0)

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