+ Reply to Thread
Results 1 to 20 of 20

VBA Code - If cell value = "xxx" then cell value = "yyy"

Hybrid View

honger VBA Code - If cell value =... 02-20-2014, 11:24 PM
ajryan88 Re: VBA Code - If cell value... 02-21-2014, 12:31 AM
honger Re: VBA Code - If cell value... 02-21-2014, 01:45 AM
ajryan88 Re: VBA Code - If cell value... 02-21-2014, 01:46 AM
honger Re: VBA Code - If cell value... 02-21-2014, 01:53 AM
honger Re: VBA Code - If cell value... 02-21-2014, 01:55 AM
honger Re: VBA Code - If cell value... 02-21-2014, 01:57 AM
ajryan88 Re: VBA Code - If cell value... 02-21-2014, 01:58 AM
ajryan88 Re: VBA Code - If cell value... 02-21-2014, 02:08 AM
honger Re: VBA Code - If cell value... 02-21-2014, 02:17 AM
ajryan88 Re: VBA Code - If cell value... 02-21-2014, 02:42 AM
ajryan88 Re: VBA Code - If cell value... 02-21-2014, 02:42 AM
ajryan88 Re: VBA Code - If cell value... 02-21-2014, 02:43 AM
honger Re: VBA Code - If cell value... 02-21-2014, 10:58 AM
honger Re: VBA Code - If cell value... 02-21-2014, 11:23 AM
honger Re: VBA Code - If cell value... 02-21-2014, 12:59 PM
ajryan88 Re: VBA Code - If cell value... 02-21-2014, 02:47 PM
ajryan88 Re: VBA Code - If cell value... 02-21-2014, 02:54 PM
ajryan88 Re: VBA Code - If cell value... 02-23-2014, 06:14 PM
honger Re: VBA Code - If cell value... 02-23-2014, 10:27 PM
  1. #1
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    VBA Code - If cell value = "xxx" then cell value = "yyy"

    I currently have this formula for each cell in a column J

    =IF(F3="Content Final","Ready for Processing","") and so on

    But i would like this to be run off of VBA code since i do not want users to delete the formula in each cell. Anybody can help me with a code?

    this is the current code i am trying out to no avail.

    Private Sub Worksheet_Calculate()
    
        Dim rng As Range, cell As Range
    
        
        Set rng = Range("f3:f10")
    
        If Not rng Is Nothing Then
    
            For Each cell In rng.Cells
                If cell.Value = "Content Final" Then
                    cell.Offset(0, 4).AddComment "ready for processing"
                End If
            Next
    
        End If
    
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    Hi,

    Try using the Worksheet_Change event instead of the Worksheet_Calculate event:
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim rng As Range, cell As Range
        
        Set rng = Range("f3:f10")
    
        If Not rng Is Nothing Then
    
            For Each cell In rng.Cells
                If cell.Value = "Content Final" Then
                    cell.Offset(0, 4).AddComment "ready for processing"
                End If
            Next
    
        End If
    End Sub
    Hope this helps

  3. #3
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    thank you very much for you help, but i am getting this message?

    2-21-2014 12-43-31 AM.png

  4. #4
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    Could you please attach your workbook...?

  5. #5
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    Quote Originally Posted by ajryan88 View Post
    Could you please attach your workbook...?
    Draft-TOC-(Product)-(Applicatiion_Number)-(SCN).xlsm

    here is the attached file

  6. #6
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    so basical;ly if i choose values in column F to content final, then column J should be Ready for processing.....

    thanks

  7. #7
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    oh and password would be 'sub2011' if you need to access them

  8. #8
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    First of all, I figured you wanted it to be a comment, so in the code that I gave you, change
    cell.Offset(0, 4).AddComment "ready for processing"
    to
    cell.Offset(0, 4).Value = "ready for processing"
    Secondly, try unprotecting the VBA and run the macro again, and tell me if you get the same error message.

  9. #9
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    Delete your Worksheet_Change subroutine and replace it with mine. I believe the error is occurring because you have multiple Worksheet_Change subroutines declared. Also, make the change that I mentioned in my previous post to have the cell value change rather than the cell comment.

    Let me know

  10. #10
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    thank you very much for all your hep. but i am getting this message now? and its freezingout-of-stack.png but the value did change to "ready for processing" its just that it keeps freezing.

    i really appreciate all your help, it is 1 am my time and i am hitting the bed. Hope to see your answer tomorrow morining once i awake.

  11. #11
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    All fixed.

    This error was occurring because the procedure was being called again once we changed the value in column J, and Excel doesn't like it when a procedure calls itself.

    The solution was to disable events while the value in column J was changed.

    Hope this helps

  12. #12
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    All fixed.

    This error was occurring because the procedure was being called again once we changed the value in column J, and Excel doesn't like it when a procedure calls itself.

    The solution was to disable events while the value in column J was changed.

    Hope this helps

  13. #13
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    Sorry about the duplicate post, had a problem with the site and it uploaded twice

  14. #14
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    thank yuo very much! this forum rocks

  15. #15
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    Ajryan, sorry to bother you again, but i am now trying to offset the column to the left. So basically if values of cells in column J is "Reset to Draft" then cells in column F will equal "Draft"

    This is the current code i have but nothing is happening?

    Private Sub Worksheet_Change2(ByVal Target As Range)
        Dim rng As Range
        Dim cell As Range
        
        Set rng = Me.Range("J3:J400")
    
        If Not rng Is Nothing Then
            For Each cell In rng.Cells
                If cell.Value = "Reset to Draft" Then
                    Application.EnableEvents = False
                    cell.Offset(0, -4).Value = "Draft"
                    Application.EnableEvents = True
                End If
            Next
        End If
        
        Set rng = Nothing
    End Sub

  16. #16
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    i figured it out.

    Thanks again AJ

  17. #17
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    You're welcome.

    It isn't working because there can only be one Worksheet_Change event, so you need to combine your code into the existing subroutine rather than creating a new one:
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim rng As Range
        Dim cell As Range
        
        Set rng = Me.Range("F3:F400")
    
        If Not rng Is Nothing Then
            For Each cell In rng.Cells
                If cell.Value = "Content Final" Then
                    Application.EnableEvents = False
                    cell.Offset(0, 4).Value = "Ready for Processing"
                    Application.EnableEvents = True
               End If
            Next
        End If
        
        Set rng = Me.Range("J3:J400")
    
        If Not rng Is Nothing Then
            For Each cell In rng.Cells
                If cell.Value = "Reset to Draft" Then
                    Application.EnableEvents = False
                    cell.Offset(0, -4).Value = "Draft"
                    Application.EnableEvents = True
                End If
            Next
        End If
        
        Set rng = Nothing
    End Sub
    Give me 5 minutes and I'll rewrite your code so that it is cleaner and easier to read, and I'll post the changed code back here

  18. #18
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    Here you go, use this subroutine instead of the last one that I posted. Hope this helps
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim rngF As Range
        Dim rngJ As Range
        Dim cell As Range
        
        Set rngF = Me.Range("F3:F400")
        Set rngJ = Me.Range("J3:J400")
        
        If Not Intersect(rngF, Target) Is Nothing Then
            'Execute this code only if the user changed column F
            If Target.Value = "Content Final" Then
                'This will only change the single affected cell, rather than searching through every cell in rngF
                Application.EnableEvents = False
                Target.Offset(0, 4).Value = "Ready for Processing"
                Application.EnableEvents = True
            End If
        ElseIf Not Intersect(rngJ, Target) Is Nothing Then
            'Execute this code only if the user changed column J
            If Target.Value = "Reset to Draft" Then
                'This will only change the single affected cell, rather than searching through every cell in rngF
                Application.EnableEvents = False
                Target.Offset(0, -4).Value = "Draft"
                Application.EnableEvents = True
            End If
        End If
        
        Set rngF = Nothing
        Set rngJ = Nothing
    End Sub

  19. #19
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    No problems, please don't forget to mark this thread as solved

  20. #20
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA Code - If cell value = "xxx" then cell value = "yyy"

    Thank u!!!

+ 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. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  2. [SOLVED] If there is any text in column "A$" on "sheet1" then move cell to column "A$" on "sheet2"
    By ckgeary in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-27-2013, 08:28 PM
  3. [SOLVED] Data validation: allow entry into a cell if other three cells have "X", "Y" and "Z"?
    By RogerRangeRover in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-05-2013, 04:49 AM
  4. Replies: 0
    Last Post: 11-20-2012, 10:22 AM
  5. Replies: 1
    Last Post: 01-30-2006, 06:10 PM

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