+ Reply to Thread
Results 1 to 11 of 11

Change one cell value based on another

Hybrid View

  1. #1
    Registered User
    Join Date
    03-16-2012
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    11

    Change one cell value based on another

    Howdy!

    I have probably a simple problem to solve but I have been around in circles and not getting anywhere!
    Basically I have a column M ("Reject" - values are 0 or 1) and another column R called "Status". I would like a command button to scroll through each row and where Col M = "1" the code will check what is in Col R and write "Cancelled".

    So where cell in Col M = "1" then cell in Col R = "Cancelled"

    I have been able to get this working (sort of) but individual cells, but fail each time I try to get it to work on the entire data range

    any help is appreciated!!!

    thanks
    Harvi

  2. #2
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Change one cell value based on another

    Hi

    Why do you need a macro for this, As you can do, using formula?
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

  3. #3
    Registered User
    Join Date
    03-16-2012
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Change one cell value based on another

    Hi Fotis,

    basically I get the data as an export from another system, when I get the data it should already be as above (where Col = 1 then other Col = "Cancelled"), but the tool is not consistent in this, so I have to filter then flood fill, and I have to do this on many other cols (but based on other criteria). So i really want to create some automation where I can tody up the data in one click

    thanks!
    Harvi

  4. #4
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Change one cell value based on another

    ..OO. I understand! So, you are welcome

  5. #5
    Registered User
    Join Date
    03-16-2012
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Change one cell value based on another

    So I have this so far - works for one cell reference but not sure how to expand it to look through all rows in the file...

    Sub Button4_Click()

    Sheets("New").Activate

    If Range("X2").Value = "1" Then
    Range("R2").Value = "Cancelled"
    Else
    End If


    End Sub

  6. #6
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Change one cell value based on another

    Try this:

    Option Explicit
    
    Sub Button4_Click()
        Dim i As Long
        
        Sheets("New").Activate
        For i = 1 To Cells.SpecialCells(xlCellTypeLastCell).Row
            If Cells(i, 24).Value = "1" Then
                Cells(i, 18).Value = "Cancelled"
            End If
        Next i
    
    End Sub
    I hope that helps.
    .?*??)
    `?.???.?*??)?.?*?)
    (?.?? (?.?
    Pichingualas <---
    ??????????????????????????

    Wrap your code with CODE TAGS.
    Thank those who helped you, Don't forget to add to their REPUTATION!!! (click on the star below their post).
    Please mark your threads as [SOLVED] when they are (Thread Tools->Mark thread as Solved).

  7. #7
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Change one cell value based on another

    By the way please check the forum rules.

    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Select your code and click the [#] button at the top of the post window (if you are editing an existing post, press Go Advanced to see the [#] button). The result will appear like this in the post window:

    [code ]
    your code here ...
    and here ...
    and here
    [ /code]

    ... and appear like this when posted:



    your code here ...
    and here ...
    and here
    You can also type the code tags in manually if you prefer.

  8. #8
    Registered User
    Join Date
    03-16-2012
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Change one cell value based on another

    will keep to the rules
    thanks for that code, worked a treat - and I think I understand it as well

    I was trying to modify it to perform a similar function, but to delete rows with certain criteria (these are rows that come from the other system erroneously and I don't need them)

    so the criteria was where the status was "Cancelled" or "Cancelled<24" (so I chose the LIKE statement, hope it works ;-) ) and where the value in column 8 is blank (again, not sure if just leaving it blank is valid?

    then I know the "entirerow.delete" will work in there somewhere but can't seemt to work it out

    I know I am a noob, but I am trying

    Sub Button5_Click()
        Dim i As Long
        
        Sheets("New").Activate
        For i = 1 To Cells.SpecialCells(xlCellTypeLastCell).Row
            If Cells(i, 18).Value Like "Cancelled" And Cells(i, 8).Value = "" Then
            
           EntireRow.Delete
                   
            End If
        Next i
    
    End Sub

  9. #9
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Change one cell value based on another

    Try like this:

    Sub Button5_Click()
        Dim i As Long
        Sheets("New").Activate
        For i = 1 To Cells.SpecialCells(xlCellTypeLastCell).Row
            If Left(Cells(i, 5).Value, 9) = "Cancelled" And Cells(i, 1).Value = "" Then
                Cells(i, 1).EntireRow.Delete
            End If
        Next i
    End Sub
    Or if you want to use Like then:

    Sub Button5_Click()
        Dim i As Long
        Sheets("New").Activate
        For i = 1 To Cells.SpecialCells(xlCellTypeLastCell).Row
            If Cells(i, 5).Value Like "Cancelled*" And Cells(i, 1).Value = "" Then
                Cells(i, 1).EntireRow.Delete
            End If
        Next i
    End Sub
    The problem with your code was that you didn't tell which line to delete. And if you dont add the * after cancelled in your like statement, it will only do it for the ones that are exactly cancelled, leaving behind the cancelled<24.

  10. #10
    Registered User
    Join Date
    03-16-2012
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Change one cell value based on another

    Thanks Pinchingualas!!

    Thanks for the reply, understand where I went wrong - thanks!
    but... (there's always a but, right? ;-) )

    I stated my original intention incorrectly, but the point is the same - I need to actually delete all rows where the status is NOT like "Cancelled" (so includes "Cancelled<24")

    I used the <> statement, but not sure if I can combine it with the "Like" statement as well?

    Also, it's kinda strange but when I run the code, it does a couple of weird things -first of all it takes maybe 10 -15 seconds and the screen flashes like crazy (looks like it is cycling through the rows a lot, or something) and then it always leaves two records behind (which match the criteria - the status is "Cancelled" and the 8th Column is blank... weird...)

    here's my code:

    
    Sub Button5_Click()
        Dim i As Long
        
        Sheets("New").Activate
        For i = 1 To Cells.SpecialCells(xlCellTypeLastCell).Row
            If Cells(i, 18).Value <> "Cancelled" And Cells(i, 8).Value = "" Then
            
           Cells(i, 8).EntireRow.Delete
                   
            End If
     Next i
    
    End Sub
    any ideas?

  11. #11
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Change one cell value based on another

    Yeah I know whats wrong here. When you delete the i row, it is possible that the next one, which would be i+1, also meets the criteria but won't get deleted cause it will have turned into the i row, meaning the code won't check it again. So we have to change the current i when the row gets deleted. Also, as the total amount of written rows in your sheet will be changing, we need to update the end of the for accordingly. When this happens, I choose to use the Do Loop strategy instead.

    Option Explicit
    
    Sub Button5_Click()
        Dim i As Long 'Is the counter for the loop, will be our line number
        Dim j As Long 'Counts the amount of active lines in sheet
        
        Sheets("New").Activate
        
        Application.Calculation = xlCalculationManual 'Will improve the time it takes to run
        Application.ScreenUpdating = False 'Will stop screen flickering
        
        i = 0
        j = Cells.SpecialCells(xlCellTypeLastCell).Row
        Do
            i = i + 1
            If Not (Cells(i, 18).Value Like "Cancelled*") And Cells(i, 8).Value = "" Then
                Cells(i, 8).EntireRow.Delete
                i = i - 1 'When we delete the row, to avoid not looking at the next one we need to go back
                j = j - 1 'When we delete the row, our sheet has less lines
            End If
            If i = j Then
                Exit Do 'If we reach the last row, the loop ends
            End If
        Loop
        
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
        
    End Sub
    If you want it not to be like "Cancelled*" then you ccan add exactly that into the code, the Not operator returns True when the statement inside is False. You could be using the <>, but you would have to change it to what I had shown you before:
    If Left(Cells(i, 5).Value, 9) <> "Cancelled"
    because you need to make sure that what you are checking is exactly "Cancelled". I mean the <> does not work as the Like statement does, in which you can put in the * at the end to say "anything can go after this", but it actually checks for exact matches.

    The screen flickering is caused by the fact that we didn't turn off the ScreenUpdating, so you are seeing it delete the rows.

    I hope that helps. Please tell me if you have any other issues with this.

+ 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