如何找到并选择VBA中的最后一列? [英] How to find and select the last column in VBA?

查看:2605
本文介绍了如何找到并选择VBA中的最后一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个查找工作表的最后一列的excel宏,然后选择整个列。但是,这一列总是不一样的 - 有些日子会列为'H',其他日子会被列为'GX',因为表中的数据不断更新。到目前为止,我已经看到如何找到最后一列,然后删除它,但是它特别指向该宏,一旦宏再次运行。我需要它总是参考最后一列,无论什么栏可能。谢谢!

I am trying to create an excel macro which finds the last column of a sheet and then selects the entire column. However, this column will always be different- some days it will be column 'H', other days will be column 'GX' as the data in the sheet is constantly updated. So far I have seen how you can find the last column and then delete it, but it specifically refers to that certain column once the macro runs again. I need it to always refer to the last column, no matter what column that may be. Thanks!

这是代码。我是VBA的新手,等等,这是通过宏记录器和我在网上找到的其他东西创建的,所以忍受我!

Here is the code. I am new to VBA, etc. and this was created through the macro recorder and other things I found online so bear with me!

`Sub Macro11()
Sheets("Sheet25").Cells(1, 1).Activate
ActiveCell.SpecialCells(xlLastCell).Select
lastCol = ActiveCell.Column
Columns("W:W").Select
Selection.Delete Shift:=xlToLeft
End Sub`


推荐答案

以下是示例代码
避免在代码中使用Select / Activate。要了解为何引用此链接

Sub Macro11()
    Dim LastCol As Long

    With ThisWorkbook.Sheets("Sheet25")
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        .Columns(LastCol).Delete
    End With

End Sub

这篇关于如何找到并选择VBA中的最后一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆