I'm trying to work between AutoLisp and Excel for a range of cells. I know how to select the cells with Excel VBA in more than one way, but I'm only able to see one way of selecting cells through AutoLisp. How would the second method, shown in the VBA Code be translated into AutoLisp?
VBA Code:
Option Explicit
Public Sub Temp()
'' Variable Declarations
Dim wb As ThisWorkbook
Dim ws As Worksheet
Dim wsRange As Range
'' Initializing Variables
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
ws.Activate
'' Method 1
Set wsRange = ws.Range("$A$1:$V$6")
wsRange.Select
Stop
ws.Cells(1, 1).Select
Stop
'' Method 2
Set wsRange = ws.Range(ws.Cells(1, 1), ws.Cells(6, 22))
wsRange.Select
End Sub
AutoLisp Code:
(setq *ExcelApp* nil)
(defun C:fcnTest(/
$ExcelFile Allwbs Workbook ;------;
Sheets Worksheet wsRange objRange ;
);Variable Declarations
;; Overhead
(setq $ExcelFile "C:\\Users\\[UserName]\\Desktop\\Test.xlsx")
(setq *ExcelApp* (vlax-get-or-create-object "Excel.Application"))
(setq Allwbs (vlax-get-property *ExcelApp* 'WorkBooks))
(setq Workbook (vlax-invoke-method Allwbs 'Open $ExcelFile))
(setq Sheets (vlax-get-property Workbook "Sheets"))
(setq Worksheet (vlax-get-property Sheets 'Item 1))
;; Method 1
(setq wsRange (vlax-get-property Worksheet 'Cells))
(setq objRange (vlax-get-property wsRange "Range" "$A$1:$V$6"))
(vlax-invoke-method objRange 'Select)
;; Method 2
;; ???
;; Releasing Excel Application
(vlax-release-object *ExcelApp*)(gc); Releasing from memory
(setq *ExcelApp* nil);--------------; Setting variable to nothing
);defun C:fcnTest
Keeping with the clean format, The code in VBA can be rewritten with some additional variables.
Rewritten VBA Code:
'' Method 2
set wsCell1 = ws.Cells(1, 1)
set wsCell2 = ws.Cells(6, 22)
Set wsRange = ws.Range(wsCell1, wsCell2)
With this change, Method 2 in the AutoLisp code can be written the same way.
AutoLisp Code:
;; Method 2
(setq objCell1 (vlax-get-property Worksheet 'Cells 1 1))
(setq objCell2 (vlax-get-property Worksheet 'Cells 6 22))
(setq objRange (vlax-get-property wsRange "Range" objCell1 objCell2))
(vlax-invoke-method objRange 'Select)