Consulting

Results 1 to 6 of 6

Thread: VBA current cell value = another cell value if criteria met

  1. #1

    VBA current cell value = another cell value if criteria met

    Hello, I've written the following program,

    Sub task3V3P3()
    
    
    Dim rng As Range
    Dim cell As Range
    
    
        Set rng = Range("Q:Q")
        
        If Not rng Is Nothing Then
            For Each cell In rng.Cells
                If cell.Offset(0, -3).Value = "Approved" Or "QUOT" Then
                    ActiveCell.Value = cell.Offset(0, 20).Value
                End If
            Next
        End If
    
    
    End Sub
    But it does not seem to work. The idea is for the current cell's value (Column Q) to be the same as another cell's value (Column BH), if the cell in column N's value is = Approved or QUOT. If the value in Column N is neither Approved or QUOT, then the current cell's value will not change at all. I would also want this formula to go on until it detects an empty cell, I think the "If not rng is nothing then" works though, if not, could someone suggest to me an alternative? Thank you so much

  2. #2
    The errors that I'm getting are,
    error1.pngerror2.png

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    OR's don't work that way


    If (cell.Offset(0, -3).Value = "Approved")  Or (cell.Offset(0, -3).Value = "QUOT") Then

    You don't need the parens around the two pieces, but I like to do it to improve readability
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Quote Originally Posted by Paul_Hossler View Post
    OR's don't work that way


    If (cell.Offset(0, -3).Value = "Approved")  Or (cell.Offset(0, -3).Value = "QUOT") Then

    You don't need the parens around the two pieces, but I like to do it to improve readability
    Thanks for the reply, not getting any errors now. But program isn't working like how it should. The values in the current cell becomes "0.00", while the rest of the cells in the column doesn't change at all. Forgot to mention that the values in column BH are from a VLOOKUP, not sure if it affects it or not though.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by jonnyjonjon View Post
    Thanks for the reply, not getting any errors now. But program isn't working like how it should. The values in the current cell becomes "0.00", while the rest of the cells in the column doesn't change at all. Forgot to mention that the values in column BH are from a VLOOKUP, not sure if it affects it or not though.
    If you post a small workbook with sample data it will be easier to see

    Click [Go Advanced] bottom right and attach a small file
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Change Activecell to your variable Cell to enter the values
    If you don't restrict "r", the code has to check all 2^20 cells in the column. There are various ways to do this e.g. finding LastRow
    Using UCase avoids error due to capitalisation of data.

    Sub task3V3P3()
        
        Dim rng As Range
        Dim cell As Range
        With ActiveSheet
        Set rng = Intersect(.UsedRange, .Range("Q:Q"))
        End With
        If Not rng Is Nothing Then
            For Each cell In rng.Cells
                If UCase(cell.Offset(0, -3).Value) = "APPROVED" Or UCase(cell.Offset(0, -3).Value) = "QUOT" Then
                    cell.Value = cell.Offset(0, 20).Value
                End If
            Next
        End If
    End Sub
    Alternative code
    Sub task3V3P4()    
        Dim rng As Range
        Dim cell As Range
        Set rng = Range("N:N").SpecialCells(xlCellTypeConstants)
        If Not rng Is Nothing Then
            For Each cell In rng.Cells
                If UCase(cell.Value) = "APPROVED" Or UCase(cell.Value) = "QUOT" Then
                    cell.Offset(, 3) = cell.Offset(0, 23).Value
                End If
            Next
        End If
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •