excel - 使用 Excel,尝试从外部 HTA 中查找真正使用过的范围

标签 excel vba vbscript hta

我一直在使用这个命令:

LastRow = ActiveSheet.UsedRange.Rows.Count

但是UsedRange 属性通常可能不准确。所以我正在寻找替代方案。我发现了一个很好的提示来解释此方法:

LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

这在 Excel 中是一种享受。但是,我正在从 HTA 运行代码,所以我需要转换这一行,但我很挣扎。这是我的问题 --- 请有人提供有关如何将这个简单代码转换为 HTA 兼容代码的指导吗?

出于兴趣,为了尝试规避这个问题,我尝试以我理解的方式编写一个例程。结果可以工作,但是速度非常慢。这是为了咯咯笑:

    Set objExcel = GetObject(,"Excel.Application") 
    wb = "test.xlsx"
    objExcel.Workbooks(wb).Activate
    wbactiveSheet = objExcel.Workbooks(wb).ActiveSheet.Name

    'determine rows and columns in range of first xls
    theNumRow = objExcel.Workbooks(wb).Sheets(wbactiveSheet).UsedRange.Rows.Count
    theNumCol = objExcel.Workbooks(wb).Sheets(wbactiveSheet).UsedRange.Columns.Count

    'determine genuine used rows:
    x = 1 'start at first row
    blankRows = 0
    Do 'each row
        y = 1 'start at first column
        blankCells = 0
        Do 'each column
            If objExcel.Workbooks(wb).Sheets(wbactiveSheet).Cells(x, y).Value <> "" Then
                theNumRowActual = x
                'found non-blank, skip to next row
                Exit Do 'the columns loop
            Else
                blankCells = blankCells + 1
            End If
            y = y + 1
        Loop Until (y - 1) = theNumCol
        If blankCells = theNumCol Then 'blank row
            blankRows = blankRows + 1
            If blankRows = 50 Then
                Exit Do
            End If
        Else
            blankRows = 0
        End If
        x = x + 1
    Loop Until x = theNumRow 'i.e. until the testing area matches the usedRange property

    'determine genuine used columns:
    y = 1 'start at first column
    blankCols = 0
    Do 'each column
        x = 1 'start at first row
        blankCells = 0
        Do 'each row
            If objExcel.Workbooks(wb).Sheets(wbactiveSheet).Cells(x, y).Value <> "" Then
                theNumColActual = y
                'found non-blank, skip to next column
                Exit Do 'the rows loop
            Else
                blankCells = blankCells + 1
            End If
            x = x + 1
        Loop Until (x - 1) = theNumRowActual
        If blankCells = theNumRowActual Then 'blank column
            blankCols = blankCols + 1
            If blankCols = 50 Then
                Exit Do
            End If
        Else
            blankCols = 0
        End If
        y = y + 1
    Loop Until (y - 1) = theNumCol 'i.e. until the testing area matches the usedRange property
    'bug


    MsgBox "USEDRANGEMETHOD:" &vbNewLine & "rows: " & theNumRow & vbNewLine & "columns: " & theNumCol & vbNewLine & vbNewLine & "NEWMETHOD:" & vbNewLine & "rows: " & theNumRowActual & vbNewLine & "columns: " & theNumColActual

谢谢

最佳答案

您的工作表似乎已经打开并处于事件状态?在这种情况下:

Const xlByRows = 1
Const xlPrevious = 2

Set objExcel = GetObject(,"Excel.Application")
LastRow = objExcel.ActiveSheet.Cells.Find("*", , , , xlByRows, xlPrevious).Row

查看我的recent post有关使用 VBScript 调用 Excel 函数的更多提示。

当您在 Excel 中使用 VBA 进行编程时,Cells 是您可以直接引用的 native 对象。但是,当您在 Excel 之外(在 HTA 或 WSH 脚本中)时,您的脚本不知道 Cells 指的是什么。您拥有的唯一 Excel 对象是您创建的对象。因此,您必须沿着链向下操作,从主 Excel (Application) 对象到工作簿,然后到工作表,然后才能对 Cells 属性进行操作。如果您使用与 Excel 相同的名称,可能会更有意义:

' Define the same objects that Excel does...
Set Application = GetObject(, "Excel.Application")
Set ThisWorkbook = Application.ActiveWorkbook

' Now get a sheet based on its name ("Sheet1", in this example)..
Set MySheet = ThisWorkbook.Sheets("Sheet1")

' And operate on the Cells collection...
MySheet.Cells.Find(...)

作为快捷方式,Excel 提供了一个 ActiveSheet 属性,您可以直接在 Application 对象上使用该属性来获取当前工作表,实质上绕过了工作簿层。这就是我在第一个代码片段中使用的内容。

关于excel - 使用 Excel,尝试从外部 HTA 中查找真正使用过的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22897490/

相关文章:

Excel VBA 突出显示事件列中的重复项

excel - 运行时错误 1004 : Clearing cell next to dropdown list

xml - 如何在 XPath 中执行不区分大小写的搜索?

class - VBA 类方法链接

xml - 在 SQL Server 中轻松使用 Excel 数据

vbscript - 了解 VBS 多行表示法

vbscript - 启动路径包含空格的程序

vba - 从命令行启动 VBA 宏 (Excel)

excel - 如何使用一组条件进行 COUNTIFS 并匹配 NOT 条件?

excel - 在另一张纸上打印,为什么它不起作用?