excel - Find LastColumn:尝试了 2 种方法,每种方法都给出不同的列作为最后
问题描述
这是我的方法#1。正确的结果应该产生第 1 行和第 384 列。这种方法给出了正确的结果,但是对于激活/选择等,它的效率非常低。
Workbooks.Open Filename:=sDest, UpdateLinks:=0
Windows(sDestFile).Activate
Sheets(sDestTab).Select
'Find LastColumn
Dim colLast As Integer
With ActiveSheet
colLast = Cells(rowTop, Columns.Count).End(xlToLeft).Column
End With
然后我创建了方法 #2。这种方法不那么碍眼,但它始终给我第 1 行和第 386 列作为答案(而不是第 384 列)。我一生都无法弄清楚为什么代码的更改会使我的 LastColumn 移动 2。
Workbooks.Open Filename:=sDest, UpdateLinks:=0
'Find Last Column
Dim colLast As Integer
colLast = Workbooks(sDestFile).Worksheets(sDestTab).Cells(rowTop, Columns.Count).End(xlToLeft).Column
解决方案
使用.Find
. 尝试这个
Dim lastcol As Long
With Workbooks(sDestFile).Worksheets(sDestTab)
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lastcol = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
Else
lastcol = 1
End If
End With
MsgBox lastcol
如果你想在第 1 行找到最后一列,那么试试这个
Dim wb As Workbook
Dim colLast As Long
Set wb = Workbooks.Open(Filename:=sDest, UpdateLinks:=0)
With wb.Sheets(sDestTab)
colLast = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
推荐阅读
- reactjs - 在 react-tabulator 列中正确显示 React 组件
- linux - 如何在 Linux 上为现有的 conda 环境添加名称?
- javascript - 无法获取 .env 变量以隐藏 api 密钥
- word2vec - 是否有任何能够检测短语的预训练 word2vec 模型
- flutter - 在颤动中检查互联网连接
- java - 当单击其中的项目时,我无法让我的底部导航视图显示新 URL。我怎样才能解决这个问题?
- sql - 在 SQL (PostgreSQL) 中,根据最接近日期值的其他字段的唯一组合对字段求和
- ios - 使用 Xcode 11 构建 iOS 应用程序后,速度要慢 20 倍以上
- laravel-artisan - 我正在尝试在命令行上运行 PHP artisan tinker 命令并显示此错误
- laravel - Laravel Cashier 不存卡版本 10.1