Locked lesson.
About this lesson
Exploring the benefits of the Immediate window for logging and querying, as well as writing when needed.
Quick reference
Using the Immediate window
Using the Immediate window.
When to use
The Immediate window allows you to change values at runtime and question the state of a variable.
Instructions
Opening the Immediate Window
- Go to the View menu on the main toolbar and select Immediate window
- Alternately you can press CTRL + G
Using the Immediate window directly
- Query anything by typing a question mark (?) followed by code pointing to an object or variables properties
- Execute a method by typing it directly into the window and pressing Enter
Writing values to the Immediate window at run time
- Insert a Debug.Print command in the code and the output will display in the Immediate window.
- Debug.Print commands can be left in the code once the routine is ready to use, but it will take extra time for them to be calculated.
- If a Debug command is left in the code, you can add a single quote (‘) at the beginning to prevent it from executing.
- 00:05 The next window I want to show you is the immediate window, down on the bottom here.
- 00:09 And we can actually pull this one up by pressing Ctrl+G or by going to the view
- 00:14 menu and saying immediate window, and that will give us this little guy here.
- 00:17 And again, he looks inspiringly blank.
- 00:19 But there's actually quite a few ways to use this guy.
- 00:21 So first thing I want to do is, I'm gonna take a look.
- 00:24 I've got a different Macro here, this one has a little bit of data that's selected.
- 00:29 And the intent of this macro is to copy this, and
- 00:32 place it in the cell immediately adjacent on the right hand side.
- 00:35 So what I'm gonna do is, I'm gonna step into this.
- 00:37 And you'll notice that because we have and rng as range variable,
- 00:42 it shows up in the locals window, so that's cool.
- 00:45 So we'll step into this, and we'll say set range equal to the selection.
- 00:50 And there we go.
- 00:51 So we could drill down into this and say rng and
- 00:54 we can see that hey look there's some cells within the range.
- 00:57 And we can see that, if we open this up just a little bit here.
- 01:00 We have a count of four cells in the range,
- 01:03 which as you can see here we definitely do.
- 01:04 So this is kind of neat.
- 01:07 We can find this out another way though.
- 01:08 We can actually go to the Immediate window, and
- 01:11 we could type in question mark space.
- 01:13 So question mark says tell me something about this.
- 01:15 I'm gonna type in rng., and it give me a list of all these things.
- 01:20 And I can type in cell.
- 01:21 Hey, look at that. It says ce, there's Cells, Tab.
- 01:25 So this will tell me the cells.
- 01:27 What do I wanna know about it?
- 01:28 Well, dot, actually, check this one out.
- 01:31 Address that's gonna need, tells me it's an A3 to A6 cool.
- 01:36 All right and it's true.
- 01:38 Let's try another one.
- 01:38 rng.cells.
- 01:40 how about counts?
- 01:42 Look at that.
- 01:44 Tells me that there's four cells in there.
- 01:46 So this is kinda cool.
- 01:47 This actually gives me some pretty neat IntelliSense that
- 01:50 I can actually work with on this.
- 01:51 Because there's all kinds of things that I can actually see in this entire list here
- 01:56 that I may or may want to do.
- 01:57 And look at that, I could get into Borders.
- 01:59 And then I could go .Color or count or whatnot I can play around
- 02:04 with all of these kind of individual components if I wanted to.
- 02:08 And this allows me to question those things.
- 02:10 So that's kinda cool.
- 02:11 Now, we can see here this is rng.copy, that's cool.
- 02:15 And then it says it's gonna go to rng.offset(0, 1) Pay special.
- 02:21 Well, now that's interesting.
- 02:22 I wonder what that is.
- 02:24 Rng.offset, open parenthesis and
- 02:27 it's going to give me a zero for a row offset comma one for
- 02:32 column offset, we'll close that off, dot address.
- 02:37 Let's go and figure out, if we can figure out where this is.
- 02:41 B3, B6.
- 02:43 So I can actually start querying that stuff from here to find out where it's
- 02:46 going to go before it goes anywhere.
- 02:48 So we'll say hey, that looks cool.
- 02:49 Lets run it.
- 02:50 Awesome, does it work with text?
- 02:54 Lets go back into our window here, and
- 02:59 we can go and step through the whole thing and indeed we can see that it does.
- 03:04 So that's kinda neat.
- 03:05 Now, what about some other things we might wanna know with this one?
- 03:10 Well, there's another way the immediate window can actually be used on here too.
- 03:13 Why don't we try this one?
- 03:14 I'm gonna use a little command here called debug.print.
- 03:18 Actually, let's put it above the rng.copy because
- 03:21 we might as well set it right after here.
- 03:23 dbug.print, rng.address and
- 03:28 dbug.print rng.count.
- 03:34 Or actually sales dot count is what I think we're looking for.
- 03:36 cellscount, there we go.
- 03:38 So, what does this do?
- 03:41 Well lets go and select two cells.
- 03:44 Actually lets grab these ones here, lets see what end up happening in this case.
- 03:48 Oops let me get back over to the visual base scanner here,
- 03:51 so ALT, F11 and now we're gonna go and step through this.
- 03:56 Set the range equal to selection,
- 03:58 debug.print feeds the value into the immediate window.
- 04:03 So this is kinda cool because when we actually go and run this stuff, its
- 04:08 still gonna copy but we can see that the items are actually being fed out there.
- 04:11 Now we could look at this one and say, you know what this isn't good enough,
- 04:16 let's say let's put in some quotes and say, address colon quote &.
- 04:21 And we could put something in
- 04:25 here that says Cells: &.
- 04:30 And now, let's go and clear the immediate window out again.
- 04:33 Let's go and grab these guys here.
- 04:36 Actually, let's grab these guys right there.
- 04:38 There we go.
- 04:39 And we'll go back over to the immediate, back over to the VBA window.
- 04:43 We'll run this again.
- 04:44 And we can see that it copied from C5 to C11 and it copied 7 cells.
- 04:48 So this is kind of cool.
- 04:50 You can leave these commands in your code,
- 04:52 although you would probably prefer not to because it does take extra time for
- 04:56 these things to actually go through and be calculated.
- 04:59 Something else that's actually kind of interesting with this too,
- 05:02 is that this immediate window is actually read write.
- 05:04 So if I were to say selection.value= Hello and hit enter.
- 05:12 It will go on right that directly back into the worksheet as well.
- 05:16 So I can change things there too.
- 05:18 So the immediate window has the ability to write back to this worksheet.
- 05:23 We can use it to change values at run time.
- 05:25 While we're in run time or even when we're not, we can actually use the immediate
- 05:29 window to question the various states of different objects.
- 05:32 So one thing I will say for your debugging purposes this is all good,
- 05:35 if you wanna leave you debugging code in there afterwards,
- 05:38 you can always go through and put in your little less single quotes before hand,
- 05:42 that will actually comment the code out, so now if you were to go and copy this
- 05:46 particular guy here, run this again and we're gonna run the copy right routine,
- 05:52 here we go it copies a values over the right field on those
- 05:55 it didn't print anything to me on window because I'd comment to these lines out
Lesson notes are only available for subscribers.