+ Reply to Thread
Results 1 to 7 of 7

if cell value is greater than or equal to 2 change its value to 1

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-29-2011
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    122

    if cell value is greater than or equal to 2 change its value to 1

    how do you write this in vba:

    if cell value is greater than or equal to 2 change its value to 1

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: if cell value is greater than or equal to 2 change its value to 1

    Sub ChangeIt()
    With Selection
        If .Value > 2 Then .Value = 1
    End With
    End Sub
    Gary's Student

  3. #3
    Forum Contributor
    Join Date
    06-29-2011
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    122

    Re: if cell value is greater than or equal to 2 change its value to 1

    what about a worksheet function?

  4. #4
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: if cell value is greater than or equal to 2 change its value to 1

    With a worksheet function you would be putting 1 in a separate cell.

    if A1 has the value, then in B1:

    =IF(A1>=2,1,A1)

  5. #5
    Forum Contributor
    Join Date
    06-29-2011
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    122

    Re: if cell value is greater than or equal to 2 change its value to 1

    Quote Originally Posted by Jakobshavn View Post
    With a worksheet function you would be putting 1 in a separate cell.

    if A1 has the value, then in B1:

    =IF(A1>=2,1,A1)
    Thanks, seperate col is not a problem.


    When i tried it in VBA it didnt work out for me...can you see what ive done wrong? ...

    Workbooks("dataset1.xls").Sheets("Sheet3").Activate
    Range("F2:F11").Select
    
    With Selection
        If .Value > 2 Then .Value = 1
    End With
    
    End Sub
    doesnt like the range i think.

  6. #6
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: if cell value is greater than or equal to 2 change its value to 1

    We need to loop over the cells in the range:

    Sub Demo()
    For Each cel In Workbooks("dataset1.xls").Sheets("Sheet3").Range("F2:F11")
        With cel
            If .Value > 2 Then .Value = 1
        End With
    Next cel
    End Sub

  7. #7
    Forum Contributor
    Join Date
    06-29-2011
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    122

    Re: if cell value is greater than or equal to 2 change its value to 1

    brilliant thanks Jake!

+ 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