From: Rodby on 13 Mar 2010 17:17 I am trying to learn VBA from a STEP BY STEP written by Reed Jacobson. He encourages using the Immediate Window because he claims you can see the progress as you code. I am copying his code verbatim. For example, in the current practice, I have the following Macro shell; Sub PivotSet Style() Dim pt as Pivottable Dim ts as Tablestyle Set pt=ActiveCell.Pivottable End sub I initialize this by pressing F8 three times, and then I type the following in the immediate window: Set ts=ActiveWorkbook.Tablestyles("PivotStyleDark2").Duplicate("NewPivotStyle") pt.Tablestyle2=ts.Name (Note: the "Set ts= ActiveWorkbook..." statement above, is all on one line in the immediate window) When I get to the last line I get an "Object Required" error message. I've checked the code and the instructions, and I cannot see anything different from what I have copied. What am I doing wrong?
From: joel on 14 Mar 2010 06:31 the code is not written very well. First, don't use "activecell". for this code to work you need 3 things to happen 1) You need to have a pivot table 2) You need to have the sheet with the pivot table as the active sheet 3) You need to have the active cell on the worksheet select a cell inside the pivot table. The line should be like this from Set pt=ActiveCell.Pivottable to Set pt = sheets("sheet1").Range("A1").Pivottable -- joel ------------------------------------------------------------------------ joel's Profile: 229 View this thread: http://www.thecodecage.com/forumz/showthread.php?t=187304 http://www.thecodecage.com/forumz/chat.php
From: Rodby on 14 Mar 2010 13:24 Joel, thank you! to be fair to the author I copied this from, there was a pivot table on an active sheet. I do not know if I had actually was in a cell in the table. Is there something about the immediate window that I am missing? I rarely get it to work, and it often gives me "undefined" type messages. "joel" wrote: > > the code is not written very well. First, don't use "activecell". for > this code to work you need 3 things to happen > > 1) You need to have a pivot table > 2) You need to have the sheet with the pivot table as the active sheet > 3) You need to have the active cell on the worksheet select a cell > inside the pivot table. > > The line should be like this > > from > Set pt=ActiveCell.Pivottable > to > > Set pt = sheets("sheet1").Range("A1").Pivottable > > > -- > joel > ------------------------------------------------------------------------ > joel's Profile: 229 > View this thread: http://www.thecodecage.com/forumz/showthread.php?t=187304 > > http://www.thecodecage.com/forumz/chat.php > > . >
From: joel on 14 Mar 2010 14:43 I don't use the immediate window often. I prefer to add watch items when I debug. the few times I have used it I didn't have any problems, but I know the correct syntax of the the statments. Most problems with beginners is they don't which object has the focus or is the active object. Using Activecell and active sheet causes problem because excel changes the active object when you aren't aware that it is being changed. I avoid using ActiveCell, Selection, and Activesheet. ther are bugs in VBA where you must use these features and know when I must use them from experience. I don't need to use the immediate window because I can read and write obejct using the watch window and I don't have to type the name of the object al I have to do is highlight the object name in the code and then right click the mouse. If I have to read or write to the worksheet I put a break point in the code and then go to the worksheet and look at the cells or change the cells as needed. I only use the immediate window when I having problems and to verify the results I'm getting using my other debugging techniques. Or when I'm helping other people who don't want to change their code and have used ActiveCell, and Activehsheet. -- joel ------------------------------------------------------------------------ joel's Profile: 229 View this thread: http://www.thecodecage.com/forumz/showthread.php?t=187304 http://www.thecodecage.com/forumz/chat.php
From: Rodby on 14 Mar 2010 18:40
Joel, thanks again! I think I will be back for more as I get further along. I find the help routines in Excel VBA almost worthless. It is though you go to a restaurant and you see the menu, but you don't know what "medium rare" means, or what "side items" mean, and there is nothing/nobody to help you find out. Anyway, thanks for the tips! "joel" wrote: > > I don't use the immediate window often. I prefer to add watch items > when I debug. the few times I have used it I didn't have any problems, > but I know the correct syntax of the the statments. > > Most problems with beginners is they don't which object has the focus > or is the active object. Using Activecell and active sheet causes > problem because excel changes the active object when you aren't aware > that it is being changed. > > I avoid using ActiveCell, Selection, and Activesheet. ther are bugs in > VBA where you must use these features and know when I must use them from > experience. I don't need to use the immediate window because I can read > and write obejct using the watch window and I don't have to type the > name of the object al I have to do is highlight the object name in the > code and then right click the mouse. If I have to read or write to the > worksheet I put a break point in the code and then go to the worksheet > and look at the cells or change the cells as needed. > > I only use the immediate window when I having problems and to verify > the results I'm getting using my other debugging techniques. Or when > I'm helping other people who don't want to change their code and have > used ActiveCell, and Activehsheet. > > > -- > joel > ------------------------------------------------------------------------ > joel's Profile: 229 > View this thread: http://www.thecodecage.com/forumz/showthread.php?t=187304 > > http://www.thecodecage.com/forumz/chat.php > > . > |