gpt4 book ai didi

excel - 使用 VBA 选择表

转载 作者:行者123 更新时间:2023-12-04 22:18:10 29 4
gpt4 key购买 nike

我想使用 VBA 选择整个表。在不使用 VBA 的情况下,我可以使用 Ctrl+A 函数并选择整个表格。我尝试了另一种方法,但仍然无济于事。
第一种方法:

Dim last_row As Long
Dim last_column As Long

Sheets("sheet1").Select
last_row = Cells(Rows.Count, 1).End(xlUp).Row


last_column = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

Range("A1:last_row & last_column").Select
'ActiveCell.Offset(last_row, last_column).Range("A1").Select
方法二:
Sheets("Sheet1").Select

Dim CSLastRow As Long
Dim CSLastColIndex As Long
Dim Last As Long

'finding last row
CSLastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
'finding last col
CSLastColIndex = Cells.Find(What:="I will recommend this course to others.", _
after:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
ColumnLetter = Split(Cells(1, CSLastColIndex).Address, "$")(1)

Last = concatenate(ColumnLetter, CSLastRow)

Range("A1:" & Last).Select

最佳答案

您的两个代码中都存在语法错误,因此它们会产生错误。我不是建议您使用任何新代码,而是修复您的原始代码,看看它们是否适合您。

Sub Macro1()
Dim last_row As Long
Dim last_column As Long

Sheets("sheet1").Select
last_row = Cells(Rows.Count, 1).End(xlUp).Row


last_column = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

Range("A1", Cells(last_row, last_column)).Select

End Sub


Sub Macro2()
Sheets("Sheet1").Select

Dim CSLastRow As Long
Dim CSLastColIndex As Long
Dim Last As String
Dim ColumnLetter As String

'finding last row
CSLastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row

'finding last col
CSLastColIndex = Cells.Find(What:="I will recommend this course to others.", _
after:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
ColumnLetter = Split(Cells(1, CSLastColIndex).Address, "$")(1)

Last = ColumnLetter & CSLastRow

Range("A1:" & Last).Select
End Sub

关于excel - 使用 VBA 选择表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66756977/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com