0

Hi i'm new in VBA can you please explain why my debug.print give error? Thank you

Sub Macro1()

Dim ws As Worksheet

i = Worksheets.Count
Set ws = Worksheets(i)

Debug.Print ws



End Sub
2
  • what property of ws are you interested in? You can't print the actual worksheet object Commented Mar 22, 2018 at 8:55
  • you are probably looking for the sheet's name, so try Debug.Print ws.Name Commented Mar 22, 2018 at 8:56

1 Answer 1

2

You can't print the worksheet object (ws). You can print some of it's properties e.g. it's name. Also, put Option Explicit at the top of your module and declare i.

You can use Debug.Print to write out information, to help with debugging, to the immediate window. The immediate window can be opened with Ctrl + G.

You can gain the same information by putting ? in the immediate window before the same code line e.g. ?ws.Name (provided your code has been halted during execution, example, with STOP keyword, and has retained the required value)

Option Explicit

Sub Macro1()

Dim ws As Worksheet
Dim i As Long

i = Worksheets.Count
Set ws = Worksheets(i)

Debug.Print ws.Name


End Sub
Sign up to request clarification or add additional context in comments.

18 Comments

LOL, typed it in at the same second
:-) OMG.... for once, I shall just savour this moment, I got answer in before The Rado! You must have been multi-tasking!
@QHarr So how debug.print even work how should i use it? And ty for answer
@QHarr Thank you for your time :) please don't delete any of your comments i would like o save the sites when i get home :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.