在vba上为“with”函数添加代码

问题描述 投票:0回答:1

我有这个:

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
vba excel-vba excel
1个回答
0
投票

你只需要在With Sheets("MOV MERCADERIA")之前的End Withand之后插入代码,用Cells替换.Cells,用range替换.range,用rows替换.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

© www.soinside.com 2019 - 2024. All rights reserved.