Excel VBAselect范围在最后一行和一列

我试图创build一个macro ,select最后一行和最后一列的范围。

例如,我想从我的电子表格中select1,2,3,4,然后删除select。

数据:

 John | 10 | 10 | 10 Smith | 5 | 5 | 5 Fred | 8 | 8 | 8 1 | 2 | 3 | 4 

这是我的代码,它只selectA列的最后一行。 (select1并删除它)。 我需要它来select1到4,并删除整个行。

 Range("A" & Rows.Count).End(xlUp).Select Selection.Delete Shift:=xlUp 

这是你正在尝试? 我已经评论了代码,以便您不会有任何理解它的问题。

 Sub Sample() Dim ws As Worksheet Dim lRow As Long, lCol As Long Dim rng As Range '~~> Set this to the relevant worksheet Set ws = [Sheet1] With ws '~~> Get the last row and last column lRow = .Range("A" & .Rows.Count).End(xlUp).Row lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column '~~> Set the range Set rng = .Range(.Cells(lRow, 1), .Cells(lRow, lCol)) With rng Debug.Print .Address ' '~~> What ever you want to do with the address ' End With End With End Sub 

顺便说一句,我假设LastRow是相同的所有行和相同的列。 如果不是这种情况,那么你将使用。find最后一行和最后一列。 你可能想看到这个

最简单的修改(对你的问题的代码)是这样的:

  Range("A" & Rows.Count).End(xlUp).Select Selection.EntireRow.Delete 

可以简化为:

  Range("A" & Rows.Count).End(xlUp).EntireRow.Delete