Thread: for each error
View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson[_3_] Jim Thomlinson[_3_] is offline
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?