Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default for each error

I'm getting a variable not defined error using the following code:

Option Explicit
Private Sub UserForm_Activate()
For Each cell In Range("cpName")
If cell.Value < "" Then
lstMnemonic.AddItem cell.Value
End If
Next cell
End Sub

The same code works to populate another list box. Why am I getting this
error?

  #2   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default for each error

The same code works to populate another list box. Why am I getting this
error?


The difference is the "Option Explicit" statement which requires all
variables to be declared.

Try:

Option Explicit
Private Sub UserForm_Activate()
Dim cell As Range
For Each cell In Range("cpName")
If cell.Value < "" Then
lstMnemonic.AddItem cell.Value
End If
Next cell
End Sub



Regards,
KL


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default for each error

YOu have option explicit at the top of your code which means that you have to
declare all of your variables. That by the way is a good thing and maked
debugging a lot easier. To fix the code you need to declare cell as a range
object

Option Explicit
Private Sub UserForm_Activate()
dim cell as range 'Here is the range object declaration
For Each cell In Range("cpName")
If cell.Value < "" Then
lstMnemonic.AddItem cell.Value
End If
Next cell
End Sub

"xlcharlie" wrote:

I'm getting a variable not defined error using the following code:

Option Explicit
Private Sub UserForm_Activate()
For Each cell In Range("cpName")
If cell.Value < "" Then
lstMnemonic.AddItem cell.Value
End If
Next cell
End Sub

The same code works to populate another list box. Why am I getting this
error?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default for each error

thanks for the reply. I thought I had used option explicit in the other code
as well so it didn't make sense to me that one would give me the error and
the other wouldn't. But as it turns out, I was wrong. I had neglected to
include option explicit. THanks again.

"KL" wrote:

The same code works to populate another list box. Why am I getting this
error?


The difference is the "Option Explicit" statement which requires all
variables to be declared.

Try:

Option Explicit
Private Sub UserForm_Activate()
Dim cell As Range
For Each cell In Range("cpName")
If cell.Value < "" Then
lstMnemonic.AddItem cell.Value
End If
Next cell
End Sub



Regards,
KL



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error: "Excel encountered an error and had to remove some formatti Carl Excel Discussion (Misc queries) 0 September 18th 06 06:39 PM
Counting instances of found text (Excel error? Or user error?) S Davis Excel Worksheet Functions 5 September 12th 06 04:52 PM
Automation Error, Unknown Error. Error value - 440 Neo[_2_] Excel Programming 0 May 29th 04 05:26 AM
Syntax Error Runtime Error '424' Object Required sjenks183 Excel Programming 1 January 23rd 04 09:25 AM


All times are GMT +1. The time now is 06:05 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"