Jump to content

Excel - show which columns are used on any chosen worksheet


 Share

Go to solution Solved by water,

Recommended Posts

Perfect.
I have modified the function so it works for a 1D array as well:

#include <Excel.au3>

Global $iSheet = 2
; create a Range holding all used cells including unused rows at the top and unused columns on the left
; See: https://www.autoitscript.com/wiki/Excel_Range#Used_Range
With $oWorkbook.Sheets($iSheet)
    $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell)
    $oRange = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column))
EndWith
; Process the Range
Global $aUsedRange = _Excel_RangeRead($oWorkbook, $iSheet, $oRange)
_ArrayDisplay($aUsedRange)
If UBound($aUsedRange, 0) = 2 Then
    For $iCol = 0 To UBound($aUsedRange, 2) - 1 ; Process all columns
        For $iRow = 0 To UBound($aUsedRange, 1) - 1 ; Process all rows
            If $aUsedRange[$iRow][$iCol] <> "" Then ; add checks for valid columns here
                ; ConsoleWrite(_Excel_ColumnToLetter($iCol + 1) & @CRLF)
                ConsoleWrite($iCol + 1 & @CRLF)
                ExitLoop ; Process next column
            EndIf
        Next
    Next
Else
    For $iRow = 0 To UBound($aUsedRange, 1) - 1 ; Process all rows
        If $aUsedRange[$iRow] <> "" Then ; add checks for valid columns here
            ; ConsoleWrite(_Excel_ColumnToLetter($iCol + 1) & @CRLF)
            ConsoleWrite("A" & @CRLF)
            ExitLoop
        EndIf
    Next
EndIf

If  this works we will add a few lines to create an array with the column names.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...