gpt4 book ai didi

excel - AutoHotKey - 如何获取列中最后使用的行的行号(自下而上)?

转载 作者:行者123 更新时间:2023-12-04 21:48:32 32 4
gpt4 key购买 nike

看起来很简单,但我没有任何成功。

非常简单,使用 AHK,我想从下往上获取工作表中最后一行的编号,其中包含一个值。我不能自上而下,因为有些行是空白的,所以必须自下而上。

我的代码遍历选定文件夹中的所有文件(仅包含 excel 文件),打开它们,然后尝试根据文件中使用的行数设置打印区域。如果 <= 43 行,则将打印区域设置为第 43 行,如果更多,则将打印区域设置为该行。

我的 test.ahk 的代码看起来像这样:

FileSelectFolder, WhichFolder  ; Ask the user to pick a folder.

Loop, %WhichFolder%\*.*
{

Xl := ComObjActive("Excel.Application")
Xl.Workbooks.open(A_LoopFileFullPath) ; open workbook
Xl.Visible := 1 ; make Excel Application visible

Xl.DisplayAlerts := False

; First check if it's the dumb file that opens first, not part of the folder
if xl.ActiveWorkbook.Name = "test.ahk" {
xl.Quit()
continue
}

; Wait for the workbook to open
Sleep, 2000

; Always start on the first worksheet
Xl.Worksheets(1).Activate

; Now find the last used row for the print area
; If the last used row is on page 1, set it to the 43rd row

lastRow := xl.ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
;MsgBox, %lastRow%


; Set the print area
if lastRow <= 43 {
xl.ActiveSheet.PageSetup.PrintArea := "A1:K43"
else {
xl.ActiveSheet.PageSetup.PrintArea := "A1:K"lastRow
}



; Wait to close the script if you want for testing
Sleep, 20000


; Close and save the workbook
xl.ActiveWorkbook.Close(1)

Xl.DisplayAlerts := True


}
ExitApp

...我收到了这个错误:

Error

我已经尝试了许多其他组合,试图让最后一行也有一个值,但都没有成功。任何帮助将不胜感激。谢谢。

最佳答案

更新!
多亏了上面的帮助,找到了一个有效的公式:

xlUp := -4162
BottomRow := xl.Sheets(1).Rows.Count
LastRow1 := xl.Sheets(1).Range("B" BottomRow).End(xlUp).Row

关于excel - AutoHotKey - 如何获取列中最后使用的行的行号(自下而上)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59449220/

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