+ Reply to Thread
Results 1 to 6 of 6

Adding Else into an If statement

Hybrid View

  1. #1
    Registered User
    Join Date
    07-22-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    85

    Adding Else into an If statement

    Hi,

    I have a formula that currently replys OK if it captures "C to BBNT" or "Will C SD" on Col D. However I want to add another line that gives me "NOT OK" it it is anything else. Thanks for anyones' help in advance

    Public Sub NT_comment()
    Dim c_ell As Range, Date_Ref As Date, S_heet As Worksheet, c_ell2 As Range
    
    For Each S_heet In ActiveWorkbook.Sheets
    If Left(S_heet.Name, 3) = "ACD" Then
        S_heet.Select
        For Each c_ell In Range("D2", Cells(Rows.Count, 2).End(xlUp))
                If c_ell = "C to BBNT" Then
                    c_ell.Offset(0, 1) = "OK"
                If c_ell = "Will C SD" Then
                    c_ell.Offset(0, 1) = "OK"
                End If
        Next c_ell
    End If
    Next S_heet
    End Sub
    Attached Files Attached Files
    Last edited by kchm_2000; 10-18-2011 at 11:39 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,463

    Re: Adding Else into an If statement

    One way:

    Public Sub NT_comment()
    Dim c_ell As Range, Date_Ref As Date, S_heet As Worksheet, c_ell2 As Range
    
    For Each S_heet In ActiveWorkbook.Sheets
        If Left(S_heet.Name, 3) = "ACD" Then
            S_heet.Select
            For Each c_ell In Range("D2", Cells(Rows.Count, 4).End(xlUp))
                Select Case c_ell.Value
                    Case "C to BBNT": c_ell.Offset(0, 1) = "OK"
                    Case "Will C SD": c_ell.Offset(0, 1) = "OK"
                    Case Else: c_ell.Offset(0, 1) = "Not OK"
                End Select
            Next c_ell
        End If
    Next S_heet
    End Sub

    Think you were specifying the wrong column [Cells(Rows.Count, 4).End(xlUp))]


    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Adding Else into an If statement

    Try this
    Public Sub NT_comment()
        Dim c_ell As Range, Date_Ref As Date, S_heet As Worksheet, c_ell2 As Range
    
        For Each S_heet In ActiveWorkbook.Sheets
            If Left(S_heet.Name, 3) = "ACD" Then
                With S_heet
                    For Each c_ell In .Range("D2:D" & .Range("D" & .Rows.Count).End(xlUp).Row)
                        If c_ell = "C to BBNT" Or c_ell = "Will C SD" Then
                            c_ell.Offset(0, 1) = "OK"
                        Else
                            If c_ell <> "" Then c_ell.Offset(0, 1) = "Not OK"
                        End If
                    Next c_ell
                End With
            End If
        Next S_heet
    End Sub
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Adding Else into an If statement

    Sub snb()
     For Each sh In Sheets
       If Left(sh.Name, 3) = "ACD" Then
        For Each cl In sh.Cells(2, 4).Resize(sh.Cells(Rows.Count, 2).End(xlUp).Row - 1)
         cl.Offset(, 1).Value = IIf(cl = "C to BBNT" Or cl = "Will C SD", "", "Not ") & "OK"
        Next
       End If
     Next
    End Sub



  5. #5
    Registered User
    Join Date
    07-22-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    85

    Re: Adding Else into an If statement

    thx very much the codes work well

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,463

    Re: Adding Else into an If statement

    You're welcome.

+ 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