使用Excel,尝试从外部HTA中找到真正使用的范围

时间:2014-04-06 17:34:07

标签: 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

谢谢

1 个答案:

答案 0 :(得分:2)

看起来您的工作表已经打开并处于活动状态?在那种情况下:

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是一个可以直接引用的本机对象。但是当您在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对象上使用该属性来获取当前工作表,基本上绕过工作簿层。这就是我在第一个代码片段中使用的内容。