How to debug code with the immediate window

embekay

New Member
Joined
Aug 12, 2013
Messages
7
Hello fellow Excel friends,

A co-worker of mine was helping me out with a piece of code and used the immediate window to quickly print out properties and evaluations of expressions without needing to use a msgbox. I am trying to figure out how to do just that.

From what I know, all you are suppose to do is lead what ever you want printed out with a '?', yet when I do this I keep getting the following compile error: method not valid without suitable object.

What am I doing wrong? Could you also provide a workable example so that I can test it out on my end and make sure it's just a syntax error.

Thanks.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try this code in a new workbook. It puts numbers 1-10 in A1:A10 and loops through each cell. Step through the code using F8 and the comments show examples of what you can type into the Immediate window.
Code:
Sub Test()

    Dim r As Range, c As Range
    
    Set r = Range("A1:A10")
    r.Value = Application.Transpose(Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
    ' ?r.address
    ' ?r.parent.name
    For Each c In r
        ' ?c.address,c.value
    Next
    
End Sub
 
Upvote 0
Welcome to the Board!

Another way to use the Immediate window is to use Debug.Print. E.G.

Debug.Print "whatever you would have put in a message box previously..."

Or in the case of John's example:

Debug.Print r.address
Debug.Print r.parent.name

HTH,
 
Upvote 0
The "?" resulted in the same error as before, but the debug.print worked for me. Thanks for the replies.
 
Upvote 0
The code would actually need to be running, but 'paused' if you are referring to objects within the code.
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,634
Members
449,460
Latest member
jgharbawi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top