【问题标题】:Excel VBA - Select All Columns & Rows After Last Occupied Column & RowExcel VBA - 在最后占用的列和行之后选择所有列和行
【发布时间】:2018-10-10 22:40:45
【问题描述】:

我试图找出标识工作表中最后一个占用列的代码,然后删除之后的所有列。我还试图找出标识工作表中最后占用行的代码,然后删除之后的所有行。

到目前为止,我的想法是我需要使用Rows.Count 函数,但我无法让它选择最后一个占用行之后的整个行。列也一样...这是我到目前为止所提出的,它只标识最后一个占用的行。有谁知道如何让它在此之后现在选择整行?同样对于整个列?我似乎无法弄清楚代码来选择最后占用的列,就像我可以用行一样......提前谢谢你

行: Sheets("ppCopy").Cells(Rows.Count, 1).End(xlUp).Row

【问题讨论】:

  • 如果您最后占用的列可以在任何行中,反之亦然,那么您最好的选择是使用 Find rondebruin.nl/win/s9/win005.htm
  • 还有,为什么要选择它们?
  • @SJR 我想在之后将它们全部删除。我的文件很大,我发现如果我删除所有空白的列和所有行,我的文件大小会大大缩小。似乎是 Excel 的一个普遍问题。
  • @Sjr 想通了!必须做 ActiveSheet.Cells(LastRow, LastColumn).Select 然后将其用作 McRitchie 代码中的活动单元

标签: vba excel


【解决方案1】:

您需要阅读代码并尝试理解它,答案就会出现。

分解你已经知道的。

Sheets("ppCopy").Cells(Rows.Count, 1).End(xlUp).Row

Rows.Count = 最后可用行

End(xlUp) = 从最后一个可用行 (Rows.Count) 向上移动到最后使用的行。

那你怎么得到这个专栏呢?

Sheets("ppCopy").Cells(1, Columns.Count).End(xlToLeft).Column

Columns.Count = 最后一个可用列

End(xlToLeft) = 从最后一个可用列 (Columns.Count) 向左移动到最后一个使用的列。

以下是删除未使用的行和列的方法:

Sub deleteUnused()
    Dim lastRow As Long, lastColumn As Integer 'there are a lot of rows compared to columns
    Dim lastLetter As String, firstLetter As String
    Set wk = ThisWorkbook
    With wk.Sheets("Sheet1")
        'Get last used rows and columns based on valued from Column A and Row 1
        lastRow = .Cells(Excel.Rows.Count, 1).End(Excel.xlUp).Row
        lastColumn = .Cells(1, Excel.Columns.Count).End(Excel.xlToLeft).Column
        'Delete Rows
        .Rows("" & (lastRow + 1) & ":" & Excel.Rows.Count & "").EntireRow.Delete Shift:=xlUp
        'Delete columns, tricky because you need the Column Letters instead of the numbers
        lastColumn = lastColumn + 1
        firstLetter = Split(.Cells(, lastColumn).Address, "$")(1)
        lastLetter = Split(.Cells(, Excel.Columns.Count).Address, "$")(1)
        .Columns("" & firstLetter & ":" & lastLetter & "").EntireColumn.Delete Shift:=xlLeft
    End With
End Sub

【讨论】:

  • 嗯...我试图做 Cells(Rows.Count + 1, 1).End(xlDown).Row 认为它会识别最后一行然后选择下一行并从中选择所有但它似乎没有用。这是让它在最后占用的行之后选择下一行的错误方法吗?
  • 在获得最后一行后添加 +1。不是在你得到它的时候。 lastRow = Sheets("ppCopy").Cells(Rows.Count, 1).End(xlUp).Row 然后 lastRow = lastRow + 1
  • 感谢 Ricardo 提供的代码。明天我进去后会测试一下……不过,到目前为止,我从你的帖子中学到了很多东西!
【解决方案2】:

J 多伊 这是一个代码,它将在任何列的任何单元格中搜索值,然后选择并删除之后的所有行并对列执行相同的操作。没有非常技术或其他东西,但会做这项工作并给你一些想法。它还将包括显示空单元格的公式。

希望对您有所帮助。

Sub DeleteRowAndColumns()

With ThisWorkbook.Sheets("ppCopy")
    'find any cell no empty in a row in any row
    .Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Offset(1, 0).EntireRow.Select
    'select all the rows
    .Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete ' clear all the rows
    'find the last value in any column
    .Cells.Find(What:="*", SearchOrder:=xlColumns, SearchDirection:=xlPrevious, LookIn:=xlValues).Offset(0, 1).EntireColumn.Select
    'select columns
    .Range(Selection, Selection.End(xlToRight)).Select
    Selection.Delete 'clear columns
End With

结束子

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 2022-10-24
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多