Jump to content

[Solved] Create an array from filtered Excel sheet


UEZ
 Share

Recommended Posts

What is the best way to create an array from a filtered Excel sheet?

Let say I've set a filter this way

_Excel_FilterSet($oWorkbook, Default, Default, 10, "AutoIt", $xlFilterValues)

How can I create an array only from the filtered values only using a range from "T2:ACxxx" whereas xxx is the last line of the selection?

 

Thanks.

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

This will grab all visible rows from a range:

$oRange = $oAppl.ActiveSheet.Range("a1:E100")
For $oRow In $oRange.Rows
    If $oRow.Hidden = False Then
        ConsoleWrite($oRow.Cells(1, 1).Value & @CRLF)
    EndIf
Next

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thanks water but it doesn't work,

In Excel after filtering I get the information in the buttom: 620 of 2833 RECORDS FOUND.

But when I count it using

_Excel_FilterSet($oWorkbook, Default, Default, 19, "Network", $xlFilterValues)

Local $oRgn = $oWorkbook.Activesheet.Range("a2:a2833")

$i = 0
For $oRow In $oRgn.Rows
    If $oRow.Hidden = False Then
        $i += 1
        ConsoleWrite($i & " = " & $oRow.Cells($i, 19).Value & @CRLF)
    EndIf
Next

then the last $i is 68 only.

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

How do you define $oRgn?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Ok, my mistake. A2833 was set in ConsoleWrite and not in $oRgn.

 

Anyhow, the output is 

1 = Client Name
...
501 = DC OPS
502 = 
503 = 
504 = 
505 = 
506 = 
507 = 
508 = 
509 = 
510 = 
511 = 
512 = 
513 = 
514 = 
515 = 
516 = 
517 = 
518 = 
519 = 
520 = 
521 = 
522 = 
523 = 
524 = 
525 = 
526 = 
527 = 
528 = 
529 = 
530 = 
531 = 
532 = 
533 = 
534 = 
535 = 
536 = 
537 = 
538 = 
539 = 
540 = 
541 = 
542 = 
543 = 
544 = 
545 = 
546 = 
547 = 
548 = 
549 = 
550 = 
551 = 
552 = 
553 = 
554 = 
555 = 
556 = 
557 = 
558 = 
559 = 
560 = 
561 = 
562 = 
563 = 
564 = 
565 = 
566 = 
567 = 
568 = 
569 = 
570 = 
571 = 
572 = 
573 = 
574 = 
575 = 
576 = 
577 = 
578 = 
579 = 
580 = 
581 = 
582 = 
583 = 
584 = 
585 = 
586 = 
587 = 
588 = 
589 = 
590 = 
591 = 
592 = 
593 = 
594 = 
595 = 
596 = 
597 = 
598 = 
599 = 
600 = 
601 = 
602 = 
603 = 
604 = 
605 = 
606 = 
607 = 
608 = 
609 = 
610 = 
611 = 
612 = 
613 = 
614 = 
615 = 
616 = 
617 = 
618 = 
619 = 
620 = 
621 =

But it must be only of the same value -> NETWORK.

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

As $oRow only consists of a single row it should be:

ConsoleWrite($i & " = " & $oRow.Cells(1, 19).Value & @CRLF)

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Ah, yes - now it works.

Thanks.

 

How can I get the number 2833 (amount of rows)?

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

$oRgn.Rows.Count

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Another option without the loop might look like this.  I think it should work but I defer to @Water in all things Excel (and au3 in general):

$oRange=$oWorkbook.ActiveSheet.Range("A2:a28337").Rows.SpecialCells($xlCellTypeVisible)
local $aResult = _Excel_RangeRead($oWorkbook, 2, $oRange)
_ArrayDisplay($aResult)

 

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

Unfortunately this doesn't work. _Excel_RangeRead only works on a single range and - in this case - will only return line 1.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

The problem is that after using the SpecialCells method, you are returned a Range of non-continuous areas, which you can't use the Rows property on.

$oRange = $oWorkbook.ActiveSheet.Usedrange.SpecialCells($xlCellTypeVisible)
Local $iRowCount = 0
For $oArea In $oRange.Areas
    $iRowCount = $iRowCount + $oArea.Rows.Count
Next
ConsoleWrite($iRowCount & @CRLF)

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Ah, thanx, I tried also the areas but it didn't work as what I did.

That means I've to run 2x the range, once to get the end and the 2nd time to create the result as an array.

 

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

The problem is that after using the SpecialCells method, you are returned a Range of non-continuous areas, which you can't use the Rows property on.

$oRange = $oWorkbook.ActiveSheet.Usedrange.SpecialCells($xlCellTypeVisible)
Local $iRowCount = 0
For $oArea In $oRange.Areas
    $iRowCount = $iRowCount + $oArea.Rows.Count
Next
ConsoleWrite($iRowCount & @CRLF)

 

Thanks for that explanation.  I had a question teed up to ask what you meant by "single range" but now I get it.  Thanks!

 

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

I think I will provide a solution to get the filtered rows in a single array and add that to the Excel wiki.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Ah, thanx, I tried also the areas but it didn't work as what I did.

That means I've to run 2x the range, once to get the end and the 2nd time to create the result as an array.

 

This example returns all visible data in one go:

$oRange = $oWorkbook.ActiveSheet.Usedrange.SpecialCells($xlCellTypeVisible)
Local $aResult[1][$oRange.Columns.Count], $aContent
; Read the data of all Ranges in the Area and concatenate the returned arrays.
For $oArea In $oRange.Areas
    $aContent = _Excel_RangeRead($oWorkbook, Default, $oArea)
    _ArrayConcatenate($aResult, $aContent)
Next
_ArrayDisplay($aResult)

Could you please test if this works for you?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Well water,

optically it seems to be in one go but when you check out the stuff under the hood it look different. ;)

I'm loading a comma seperated text file whereas line 1 is some text only and the column headers are in line 2.

You script generates all entries including a blank line 0 which is the line 1 in the text file. The generated array has 622 entries (minus empty line and minus header = 620 entries).

That's ok.

 

It takes approx. 26 seconds to generate the array!

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

26 seconds to process 2866 records?
Did you just measure the code I posted above or is this the whole script (reading the CSV, filtering etc.)?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I've added a few timers to my code. Most time is spent in _Excel_RangeRead.
Which values do you get?

Local $hSpecialCells, $iSpecialCells = 0, $hRangeRead, $iRangeRead = 0, $hArrayConcat, $iArrayConcat = 0
$hSpecialCells = TimerInit()
$oRange = $oWorkbook.ActiveSheet.Usedrange.SpecialCells($xlCellTypeVisible)
$iSpecialCells = TimerDiff($hSpecialCells)
Local $aResult[1][$oRange.columns.Count], $aContent
; Read the data of all Ranges in the Area and concatenate the returned arrays.
For $oArea In $oRange.Areas
    $hRangeRead = TimerInit()
    $aContent = _Excel_RangeRead($oWorkbook, Default, $oArea)
    $iRangeRead = $iRangeRead + TimerDiff($hRangeRead)
    $hArrayConcat = TimerInit()
    _ArrayConcatenate($aResult, $aContent)
    $iArrayConcat = $iArrayConcat + TimerDiff($hArrayConcat)
Next
MsgBox(0, "", "SpecialCells: " & $iSpecialCells & @CRLF & _
              "RangeRead: " & $iRangeRead & @CRLF & _
              "ArrayConcat: " & $iArrayConcat)
_ArrayDisplay($aResult)

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...