; Find Last Row/Column in Excel - 2023.09.08 exceed
; command
; FLRE = find last row
; FLCE = find last column
(defun c:FLRE ()
(ex:FLRCE "R")
)
(defun c:FLCE ()
(ex:FLRCE "C")
)
(defun ex:FLRCE ( rc / xlApp xlBook xlSheet AcSheet Cells xlcols xlrows
Maxcols MaxRows ColumnInput RowInput
ColumnInt addr Rng finddirection whatyoufind
whatyoufindrow whatyoufindcol
whatyoufindtxt
)
(vl-load-com)
;-------------------------------------------------------------------------------
; Number2Alpha - Converts Number into Alpha string
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
; Num# = Number to convert
; Syntax example: (Number2Alpha 731) = "ABC"
;-------------------------------------------------------------------------------
(defun Number2Alpha (Num# / Val#)
(if (< Num# 27)
(chr (+ 64 Num#))
(if (= 0 (setq Val# (rem Num# 26)))
(strcat (Number2Alpha (1- (/ Num# 26))) "Z")
(strcat (Number2Alpha (/ Num# 26)) (chr (+ 64 Val#)))
)
)
)
; Alpha2Number - Converts Alpha string into Number
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
; Str$ = String to convert
; Syntax example: (Alpha2Number "ABC") = 731
;-------------------------------------------------------------------------------
(defun Alpha2Number (Str$ / Num#)
(if (= 0 (setq Num# (strlen Str$)))
0
(+ (* (- (ascii (strcase (substr Str$ 1 1))) 64) (expt 26 (1- Num#)))
(Alpha2Number (substr Str$ 2))
)
)
)
(setq xlApp (vlax-get-or-create-object "Excel.Application"))
(setq xlBook (vlax-get-property xlApp 'Workbooks))
(setq xlSheet (vlax-get-property xlApp 'Sheets))
(setq AcSheet (vlax-get-property xlApp 'ActiveSheet))
(setq Cells (vlax-get-property AcSheet 'Cells))
(setq xlcols (vlax-get-property AcSheet 'Columns))
(setq xlrows (vlax-get-property AcSheet 'Rows))
(setq MaxCols (vlax-get-property xlcols 'Count))
(setq MaxRows (vlax-get-property xlrows 'Count))
(cond
((= rc "R")
(setq ColumnInput (getstring "\n Enter the Column Number to find the last Row (A,B,C.. or 1,2,3..) : "))
(setq ColumnInt (atoi ColumnInput))
(if (and (= ColumnInt 0) (/= CoumnInput "0"))
(progn
(setq ColumnInput (strcase ColumnInput))
)
(progn
(setq ColumnInput (Number2Alpha ColumnInt))
)
)
(setq addr (strcat ColumnInput (itoa MaxRows)))
(setq finddirection 3)
)
((= rc "C")
(setq RowInput (getint "\n Enter the Row Number to find the last Column (1,2,3..) : "))
(setq addr (strcat (Number2Alpha MaxCols) (itoa RowInput)))
(setq finddirection 1)
)
)
(setq Rng (vlax-get-property AcSheet 'Range addr))
(setq whatyoufind (vlax-get-property Rng 'End finddirection)) ; finddirection : 1 = xlToLeft, 2 = xlToRight, 3 = xlUp, 4 = xlDown
(setq whatyoufindrow (vlax-get-property whatyoufind 'row))
(setq whatyoufindcol (vlax-get-property whatyoufind 'column))
(setq whatyoufindtxt (vlax-variant-value (vlax-get-property whatyoufind 'value)))
(if (= whatyoufindtxt nil) (setq whatyoufindtxt ""))
(vlax-invoke whatyoufind 'select) ;If you don't need to select a cell, delete this line
(cond
((= rc "R")
(princ "Last non-empty cell in column [ ")
(princ ColumnInput)
(princ " (= ")
(princ (Alpha2Number ColumnInput))
(princ " ) ] is in row [ ")
(princ whatyoufindrow)
)
((= rc "C")
(princ "Last non-empty cell in Row [ ")
(princ RowInput)
(princ " ] is in column [ ")
(princ (Number2Alpha whatyoufindcol))
(princ " (= ")
(princ whatyoufindcol)
(princ " )")
)
)
(princ " ], with value : [ ")
(princ whatyoufindtxt)
(princ " ].")
(vlax-release-object xlApp)
(princ)
)
command = FLRE
then input column number by number or alphabet