+ Reply to Thread
Results 1 to 5 of 5

Problems with For Each

  1. #1
    Forum Contributor
    Join Date
    09-19-2004
    Posts
    252

    Problems with For Each

    I have the code below but it gives me an error and tells me to define the variable 'cell' in the For Each cell In Selection part. Why is it doing that?

    Please Login or Register  to view this content.

  2. #2
    Jake Marx
    Guest

    Re: Problems with For Each

    Hi DKY,

    If you're using Option Explicit, you'll have to declare "cell" as an Object
    or Range variable. So put the following line of code above your loop and
    you should be fine:

    Dim cell As Range

    --
    Regards,

    Jake Marx
    MS MVP - Excel
    www.longhead.com

    [please keep replies in the newsgroup - email address unmonitored]


    DKY wrote:
    > I have the code below but it gives me an error and tells me to define
    > the variable 'cell' in the For Each cell In Selection part. Why is it
    > doing that?
    >
    >
    > Code:
    > --------------------
    > LRow = Cells(Rows.Count, "E").End(xlUp).Row
    > Range("A2:A" & LRow).Select
    > ' START CHANGE NUMBER TO TEXT
    > For Each cell In Selection
    > cell.NumberFormat = "@"
    > cell.Value = Trim(cell.Value)
    >
    > ' If cell.Value <> "" Then
    > ' TempString = Trim(UCase(cell.Value))
    > ' cell.Value = TempString
    > ' End If
    > Next cell
    > --------------------



  3. #3
    Forum Contributor
    Join Date
    09-19-2004
    Posts
    252
    Oh, I just removed Option Explicit. What's Option Explicit for anyway?

  4. #4
    Norman Jones
    Guest

    Re: Problems with For Each

    Hi DKY,

    The Option Explicit statement requires that all variables be declared and
    that the be declared explicitly.

    See Chip Pearson's 'Using Variables (Properly) In VBA' page at:

    http://www.cpearson.com/excel/variables.htm

    for a detailed discussion of the issue.

    Perhaps, prior to reading the above, you might profitably visit Chip's
    'Declaring Variables In VBA' page at:

    http://www.cpearson.com/excel/DeclaringVariables.htm


    ---
    Regards,
    Norman



    "DKY" <[email protected]> wrote in message
    news:[email protected]...
    >
    > Oh, I just removed Option Explicit. What's Option Explicit for anyway?
    >
    >
    > --
    > DKY
    > ------------------------------------------------------------------------
    > DKY's Profile:
    > http://www.excelforum.com/member.php...o&userid=14515
    > View this thread: http://www.excelforum.com/showthread...hreadid=393455
    >




  5. #5
    Forum Contributor
    Join Date
    02-26-2005
    Posts
    175
    DKY

    I asked the same question. Here is the answer.

    http://www.excelforum.com/showthread.php?t=384692

    this Help?
    Dave
    Quote Originally Posted by DKY
    Oh, I just removed Option Explicit. What's Option Explicit for anyway?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1