使用"添加代码到"在vba上运行

时间:2017-12-22 09:49:27

标签: vba excel-vba excel

我有这个:

Dim i As Long
Dim numFilas As Long

numFilas = Cells(Rows.Count, 1).End(xlUp).Row

For i = numFilas To 1 Step -1
    If WorksheetFunction.CountIf(Range("h:h"), Cells(i, 8)) > 1 Then
        Rows(i).Delete
    End If
Next i

我想将其添加到以下代码中以修改其他工作表(" MOV MERCADERIA")。我怎么做?

With Sheets("MOV MERCADERIA")
    For a = 11 To Range("a40").End(xlUp).Row
        fila = .Range("a1:a65536").Find("").Row
        .Cells(fila, 1) = [d7]                   'remito
        .Cells(fila, 2) = CDate([D6])            'fecha
        .Cells(fila, 3) = [D8]                   'cod proveedor
        .Cells(fila, 4) = [E8]                   'proveedor
        .Cells(fila, 5) = [D9]                   'CODIGO responsable
        .Cells(fila, 6) = [K8]                   'tipo
        For b = 0 To 17
            If Cells(a, b + 1) <> "" Then
                col = .Cells(fila, 16).End(xlToLeft).Column + 1
                .Cells(fila, col) = Cells(a, b + 1)
            End If
        Next b
        .Cells(fila, 26) = [N44]
    Next a
End With

1 个答案:

答案 0 :(得分:0)

您只需要在With Sheets("MOV MERCADERIA")之后和End With之前的某处插入代码,并将Cells替换为.Cells,将range替换为.rangerows.rows

像这样:

Dim i As Long
Dim numFilas As Long

With Sheets("MOV MERCADERIA")

    numFilas = .Cells(.Rows.Count, 1).End(xlUp).Row

    For i = numFilas To 1 Step -1
        If WorksheetFunction.CountIf(.Range("h:h"), .Cells(i, 8)) > 1 Then
           .Rows(i).Delete
        End If
    Next i

End With

现在代码将作用于工作表MOV MERCADERIA