Com o Excel, tentar encontrar uma faixa usada genuína do HTA externo
Eu tenho usado este comando:
LastRow = ActiveSheet.UsedRange.Rows.Count
Mas a propriedade UsedRange geralmente pode ser imprecisa. Então, eu estou procurando uma alternativa. Encontrei uma ótima dica para explicar esse método:
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Isso funciona bem no Excel. No entanto, estou executando o código de um HTA, portanto, preciso converter essa linha e estou com dificuldades. Esta é a minha pergunta --- alguém pode oferecer orientações sobre como converter esse código simples em um código compatível com HTA?
Por falta de interesse, para tentar contornar esse problema, tentei escrever uma rotina de uma maneira que entendesse. O resultadotrabalho, mas é muito lento. Aqui está para rir:
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
Obrigado