program skills about Microsoft Excel, Word and so on
3 views (last 30 days)
Show older comments
hi everybody,
I have some questions about MS Excel when used it in Matlab. For example, I wanna move the cursor into next cell, like move current cell to (iRow, iColumn) cell. In VBA, its syntax is like
hActiveSheet.Cells(iRow, iColumn).Select
where hActiveSheet is the handle of active sheet. So how do I use it in Matlab?
When I used it like VBA ayntax, an error occured if iRow > 1 or iColumn > 1... when iRow=1,iColumn=1, the whole sheet is selected...
Is there any suggestion? or is there any book introducing
such item?
Any help or suggestion is appreciated!
2 Comments
Stephen
on 26 Mar 2012
Here is some code that I use to access the data from cells A1 to A5 using ActiveX. The variable Activesheet is the excel worksheet as you might imagine.
get(Activesheet, 'Range', 'A1', 'A5')
Perhaps you could use this syntax with "set" instead of "get" to move the range around.
Accepted Answer
Eric
on 26 Mar 2012
I'm assuming that by "moving around", you're looking to use Matlab to control which cells are selected. Something like the following will work, assuming xlscom is the variable that contains the COM.Excel_Application object (and a worksheet is currently open, and that the active worksheet is not a Chart worksheet):
sheet = xlscom.ActiveSheet;
sheet.Range('F2:F5').Select
Hopefully that helps.
Good luck,
Eric
3 Comments
Eric
on 27 Mar 2012
I've got two books by Walkenbach on my shelf: "Excel 2003 Power Programming with VBA" and "Excel VBA Programming for Dummies". Neither is great, but they're decent. I used these books mostly when I was doing some work with custom GUIs and add-in development for Excel.
The main trick I use when working with COM for Excel and Word is to use the macro recorder. Manually perform the operation you want to code while recording a macro. Then hit Alt+F11 to open the Visual Basic Editor and inspect the code. You can usually inspect the code and translate to Matlab.
One thing to be aware of is Collection objects. These are usually plural names in VBA. You have to index into these by using their Item property rather than indexing into the Collection directly. For example, in Excel each Workbook object has a Worksheets collection member. In VBA you can index into the Worksheets by using Worksheets(3) or Worksheets("Sheet1"). This does not work with the COM interface. You need to index using Worksheets.Item(3), for example, to get the third worksheet. This means if you want to find a worksheet with a specified name you will need to loop through all of the worksheets, one Item at a time, and comparing its name to the desired name.
So I would say the best use for the books (or reference websites) is to familiarize yourself with the object structure of the program in question. This would be most important for PowerPoint since Microsoft has dropped the macro recorder for it. Once you understand the basics of the object structure, writing code is usually as simple as recording a macro and translating the code to Matlab.
-Eric
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!