+ Reply to Thread
Results 1 to 5 of 5

If / ElseIf within For Each?

Hybrid View

  1. #1
    Registered User
    Join Date
    06-26-2014
    Location
    Newark, Delaware
    MS-Off Ver
    2010 Excel
    Posts
    13

    If / ElseIf within For Each?

    Hey guys, I'm looking to put a few if/elseif conditions within a for each loop. Is this possible/ any suggestions as to how I can get it to work?

    Sub rating()

    Dim terr As Integer
    
    For Each mycell In Sheets("Orig File").Range("p8:p999999")
        If mycell.Value = "a" Then
            mycell.Value = 1
        ElseIf mycell.Value = "b" Then
            mycell.Value = 2
    
    Next mycell
    
    End sub
    Thanks for the help!

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: If / ElseIf within For Each?

    Sub test1()
    
    Dim terr As Long, mycell As Range
    
    For Each mycell In Sheets("Orig File").Range("p8:p999999")
        If mycell.Value = "a" Then
            mycell.Value = 1
        ElseIf mycell.Value = "b" Then
            mycell.Value = 2
            
        End If
    
    Next mycell
    
    End Sub

  3. #3
    Registered User
    Join Date
    06-26-2014
    Location
    Newark, Delaware
    MS-Off Ver
    2010 Excel
    Posts
    13

    Re: If / ElseIf within For Each?

    Awesome thank you! But I keep getting a runtime error '9'

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: If / ElseIf within For Each?

    If it is on this line
    For Each mycell In Sheets("Orig File").Range("p8:p999999")
    You do not have a sheet name "Orig File". Make sure you have the name on sheet collection and spelling is correct.

  5. #5
    Registered User
    Join Date
    06-26-2014
    Location
    Newark, Delaware
    MS-Off Ver
    2010 Excel
    Posts
    13

    Re: If / ElseIf within For Each?

    Ahh okay that fixed it, thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Code stepping in to elseif statement when elseif is definitely not true
    By DFrank231 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-13-2013, 05:31 PM
  2. If Elseif elseif problem
    By tjbillund in forum Excel General
    Replies: 3
    Last Post: 08-10-2011, 07:02 AM
  3. Re: IF..Then..ELSE.. ELSEIF
    By rjamison in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2005, 08:05 PM
  4. IF..Then..ELSE.. ELSEIF
    By ole_ in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-20-2005, 11:06 AM
  5. [SOLVED] If, ElseIf
    By mast in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-26-2005, 09:06 AM

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