Need VBA help with "Compile error: Variable not defined"

Mark Carlson

New Member
Joined
May 27, 2008
Messages
7
This is my first post. Greetings from Athens, TX!

I've used Excel for many years, but finally am learning VBA, using Excel Progamming for Dummies by John Walkenbach. Excel is version 2000.

I've searched extensively on Google and on the MrExcel site for the solution to my problem, but still haven't found it :confused:. Any direction would be most appreciated.

I'm trying to execute the examples in the book, and have gotten the error message "Compile error: Variable not defined" with "MyString =" highlighted with several of the examples. Could someone please tell me where I'm going astray?

Following is the latest subroutine I typed into the module, from page 124:

Option Explicit

Sub GetLength()
MyString = "Hello World"
StringLength = Len(MyString)
MsgBox StringLength
End Sub

Thank you for your help!

Mark Carlson
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try

Code:
Sub GetLength()
Dim MYstring As String
MYstring = "Hello World"
StringLength = Len(MYstring)
MsgBox StringLength
End Sub
 
Upvote 0
Welcome to the board.

Well, yes. When you googled, did you search on the term "option explicit"? That forces variable declarations.
Code:
Option Explicit
 
Sub GetLength()
    dim strMyString as string, lngStringLength as long
    strMyString = "Hello World"
    lngStringLength = Len(strMyString)
    MsgBox lngStringLength
End Sub
 
Last edited:
Upvote 0
Oops!

Code:
Sub GetLength()
Dim MYstring As String, StringLength As Integer
MYstring = "Hello World"
StringLength = Len(MYstring)
MsgBox StringLength
End Sub

The reason that you are getting these errors is that you have set 'Require variable declaration' in Tools > Options which is a very good idea. Shame Walchenbach et al. didn't think of that!
 
Upvote 0
Greg,

I didn't Google "Option Explicit" (OE). The book suggested using OE as a matter of course. I've seen that stated several other places while trying to figure this out.

Thanks, Mark
 
Upvote 0
Greg,

I'm afraid I'm not quite getting this. Do you mean I need an "=" in the dim statement? Also in your signature you have the hint about CODE tags. Would that be 010, 020, etc at the start of the lines?

Have a great evening!

Thanks again, Mark
 
Upvote 0
Option Explicit requires you to declare all your variables. In your code, you use two variables, MyString and StringLength, so you have to declare them:
Code:
Dim MyString As String, StringLength as Long
(note, you don't have to declare them as having a specific type/interface, but it's good practice to do so where possible)
The code tags are what I used above to make the code line stand out.
Regards,
Rory
 
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,413
Members
449,449
Latest member
Quiet_Nectarine_

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