program skills about Microsoft Excel, Word and so on

2 views (last 30 days)
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
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.
w sq
w sq on 27 Mar 2012
yes, you are quite right. But it's the method of how to write/get data in excel.
My question is when i write data into excel sheet(assuming the data is 2*3 cells), the cursor, you know, is in the first excel cell by default(row=1,column=1), but I wanna move it to the last cell(maybe row=3,column=4) after writing data, how to do it?

Sign in to comment.

Accepted Answer

Eric
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
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!