VBA Variable name Problem

The_Captian

New Member
Joined
Nov 18, 2011
Messages
8
Using Excel 2003 SP0

I created a variable called "comment" as a string. Later realized that "Comment" was a method I needed to use, so I changed the variable name to "myComment".
However, now every I try to use ".Comment" the VBA editor changes it to ".comment" and gives an object variable not defined error.

for example, the code:
Sheets("S1").Range(myCell).Comment.Shape.TextFrame.AutoSize = True
is changed to:
Sheets("S1").Range(myCell).comment.Shape.TextFrame.AutoSize = True

and errors.

Is there a way to force a reset of the VBA editor. I have tried closing, re-starting etc. but it refuses to let me use "Comment" as a method.

Thanks in advance for any replies.
 
You are correct, but if I paste in the line it changes to:
Range("A1").comment.Text "Hello"
and gives a "Object variable or With block variable not set" error

So it doesn't even attempt to work with the ".Comment" method.
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Even if I dim a variable called comment as a string I can still set the Comment property of a cell.

Are you sure there's nothing else going on?

PS I'm not recommending you do use comment as a variable, I was just kind of wondering what would happen.
 
Upvote 0
All i know is that the ".Comment" text becomes ".comment" when I move cursor to another line, and it throws a "variable not set" error on that line.
It only occurs in this project, the same code stays ".Comment" and works fine in other projects.

Note that using:
Range("A1").comment.Text = "Hello"
gives an "Assignment to constant not permitted" error with ".Text" highlighted in the code.

(wish it would let me attach screen-shots)
 
Upvote 0
Does Range("A1") of the active sheet (that's the active sheet according to VBA) definitely has a comment?

Does it work if you add a worksheet reference?
Code:
Worksheets("WSWhichhasacommentinA1").comment.Text "Test"
Obviously change the sheet name.
 
Upvote 0
Adding the Worksheet reference worked!
Worksheets("Output Parts").Range("A1").comment.Text = "Test me now"

".Comment" continues to change to lower case ".comment", unlike all other methods in the editor window. But it works!

I really have no clue why.

Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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