简体   繁体   中英

Excel-VBA .Range(“NamedRange”).Row Returning a Different Row Then Where Range is Located

I have the following the named range in Excel.

在此处输入图片说明

I also have the following code that works with that named range:

With wsVehicleSummary

With .Range(.Cells(.Range("VehicleList").Row, 1), .Cells(.Range("VehicleList").Row, .Range("K1").Column)).Offset(1)
    .PasteSpecial xlPasteValues
    .Resize(.Range("VehicleList").End(xlDown).Row - .Range("VehicleList").Row, 11).RemoveDuplicates 1, xlYes
    .Resize(.Range("VehicleList").End(xlDown).Row - .Range("VehicleList").Row, 11).Sort "Veh #", xlAscending, Header:=xlYes
End With

The issue arises in the Remove Duplicates call and I've narrowed it to down to the fact that .Range("VehicleList").Row is equating to 40, when it should be 20 (as can be seen in screenshot).

I have debugged this at run-time and am baffled why ?.Range("VehicleList").Row is returning 40 instead of 20.

Although, as I write this, I wonder if it has to do with how I called range in the With block and how I use Resize.

Any suggestions or ideas?

Thanks to @Rory 's comment, I made the following changes and my code now works as expected.

With wsVehicleSummary

    Dim lRowResize As Long
    lRowResize = .Range("VehicleList").End(xlDown).Row - .Range("VehicleList").Row

    With .Range(.Cells(.Range("VehicleList").Row, 1), .Cells(.Range("VehicleList").Row, .Range("K1").Column))
        .Offset(1).PasteSpecial xlPasteValues
        .Resize(lRowResize, 11).RemoveDuplicates 1, xlYes
        .Resize(lRowResize, 11).Sort "Veh #", xlAscending, Header:=xlYes
    End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM