+ Reply to Thread
Results 1 to 4 of 4

Simple Macro - If Cell Value Is "X" then clear another cell

Hybrid View

  1. #1
    Registered User
    Join Date
    09-17-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003
    Posts
    12

    Smile Simple Macro - If Cell Value Is "X" then clear another cell

    Hey guys,

    I'm rubbish at macros, I'd really appreciate someones help with a simple quick macro. I've got column B with values in, and column D with values in.

    I'm trying to make a macro that clears the cell in column D, based on the column B cell being value X. and it would do this for all cells in the whole of both columns,

    Can anyone help? I'd really appreciate it, I've been stuck for hours

  2. #2
    Registered User
    Join Date
    08-08-2011
    Location
    Warwick
    MS-Off Ver
    MS365
    Posts
    53

    Re: Simple Macro - If Cell Value Is "X" then clear another cell

    Hi,

    Try this:

    Dim x As Integer
              
               
        For x = 2 To 45  ' Change "45" to the last row in your document.
            If Range("B" & x).Value = "x" Then
            Range("D" & x).Select
            ActiveCell.ClearContents
        End If
        Next x
    This code will work if you assign it to a command button or something like that,


    Cheers,

    - Jon

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Simple Macro - If Cell Value Is "X" then clear another cell

    Try this
    Sub delete()
    
        Dim Rws As Long, Rng As Range, Inp As Integer, c As Range
    
        Inp = InputBox("Enter Value to Find", "Hello")
        Rws = Cells(Rows.Count, "D").End(xlUp).Row
    
        Set Rng = Range(Cells(1, 4), Cells(Rws, 4))
    
        For Each c In Rng.Cells
            If c.Offset(0, -2) = Inp Then c = ""
        Next c
    
    
    End Sub

  4. #4
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Simple Macro - If Cell Value Is "X" then clear another cell

    An approach using the Find method:

    Sub Test()
        Dim rngFindRange As Range
        Dim strFirstAdd As String
        Dim rngFoundRange As Range
        With Columns("B")
            Set rngFindRange = .Find("X", LookIn:=xlValues, lookat:=xlWhole)
            If Not rngFindRange Is Nothing Then
                strFirstAdd = rngFindRange.Address
                Set rngFoundRange = rngFindRange
                Do
                    Set rngFoundRange = Union(rngFindRange, rngFoundRange)
                    Set rngFindRange = .FindNext(rngFindRange)
                Loop While Not rngFindRange Is Nothing And rngFindRange.Address <> strFirstAdd
            End If
        End With
        If Not rngFoundRange Is Nothing Then
            rngFoundRange.Offset(0, 2).ClearContents
        End If
    End Sub

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

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

+ 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