+ Reply to Thread
Results 1 to 8 of 8

change value

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-07-2011
    Location
    Bhutan
    MS-Off Ver
    Excel 2007
    Posts
    244

    change value

    Hi Friends,

    I am stuck at problem, and was hoping if anybody could please help me out with a VBA code that will run with Column G.
    I have an excel sheet, in cell G2 I have Team A or Team B or Team C.
    Cell position G2 is deciding factor.If G2 is Team A then anything below it should be Team B, or if Team B then anything below it should be Team A.
    In this example file G2 value is Team A, but G3 is team B then automatically the corresponding value of C changes to "Retest on next sample".
    Then G4 is team B then automatically the corresponding value of C changes to "Retest on next sample".
    Then G7 is team C then automatically the corresponding value of C changes to "Retest on next sample".

    But if the cell is blank (that means no team) then the code will not change the cell value in Column C.

    I am attaching the example file for reference.
    Please help me friends,
    Thanks in advance,
    best regards,
    me
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: change value

    Sub a()
    LR = Cells(Rows.Count, "A").End(xlUp).Row
    team = Range("G2").Text
    For j = 2 To LR
      If Range("G" & j).Text = team Then
        Range("C" & j).Value = "Passed"
      Else
        Range("C" & j).Value = "Retest on next sample"
      End If
    Next
    End Sub
    Last edited by patel45; 09-15-2012 at 06:15 AM.
    If solved remember to mark Thread as solved

  3. #3
    Forum Contributor
    Join Date
    02-07-2011
    Location
    Bhutan
    MS-Off Ver
    Excel 2007
    Posts
    244

    Re: change value

    Thanks patel45
    It works like a charm.
    Thanks a ton !
    Best regards,
    Me.

  4. #4
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: change value

    Nice Coding patel45,

    But don't you think though that a simple formula in Cell C3, copied down, could avoid VBA?

    Something like:
    =IF(G3="","",IF(G3<>$G$2,"Retest on next sample","Passed"))
    Why am asking this is just that I believe that it makes it so much easier.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: change value

    Hi, fatalcore,

    did you delete the contents from cell G2 and run patel45īs code? According to your opening post:
    But if the cell is blank (that means no team) then the code will not change the cell value in Column C.
    Sub shortened_a()
    Dim lngCounter As Long
    For lngCounter = 2 To Cells(Rows.Count, "A").End(xlUp).Row
      If Not IsEmpty(Range("G" & lngCounter).Value) Then
        Range("C" & lngCounter).Value = IIf(Range("G" & lngCounter).Value = Range("G2").Value, "Passed", "Retest on next sample")
      End If
    Next lngCounter
    End Sub
    Sub shortened_b()
    Dim lngCounter As Long
    For lngCounter = 2 To Cells(Rows.Count, "A").End(xlUp).Row
      If Not IsEmpty(Range("G2").Value) Then
        Range("C" & lngCounter).Value = IIf(Range("G" & lngCounter).Value = Range("G2").Value, "Passed", "Retest on next sample")
      End If
    Next lngCounter
    End Sub
    @Winon:
    I agree on using a formula.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  6. #6
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: change value

    Quote Originally Posted by Winon View Post
    Nice Coding patel45,
    But don't you think though that a simple formula in Cell C3, copied down, could avoid VBA?
    yes, I agree, but I love vba and hate formulas

  7. #7
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: change value

    @ HaHoBe,

    Thank you HaHoBe.

  8. #8
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: change value

    @ patel45,

    yes, I agree, but I love vba...
    Glad to have you around then. I stand to learn a lot from you as well!
    Last edited by Winon; 09-15-2012 at 09:49 AM.

+ 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