+ Reply to Thread
Results 1 to 10 of 10

Macro - Clear cell contents based on other cell

Hybrid View

  1. #1
    Registered User
    Join Date
    02-05-2011
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    31

    Macro - Clear cell contents based on other cell

    Simple one for most on here but any help will be greatly appreciated.

    I need a macro that will look at a range (AX5:AX2000), if a cell in that range = 1 then it will delete the corresponding cell on that row in columns Q:S.

    Thanks in advance!

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro - Clear cell contents based on other cell

      Sub clear()
    
            Application.ScreenUpdating = 0
            LR = Cells.Find("*", , , , xlByRows, xlPrevious).Row
            
            For i = 5 To LR
                If Cells(i, "AX") = 1 Then Cells(i, "Q").Resize(, 3).ClearContents
            
            Next i
            
            Application.ScreenUpdating = True
    End Sub

  3. #3
    Valued Forum Contributor
    Join Date
    02-09-2012
    Location
    Mauritius
    MS-Off Ver
    Excel 2007
    Posts
    1,055

    Re: Macro - Clear cell contents based on other cell

    Hi.

    You can try this too:

    Sub delrows()
        For i = 5 To 2000
            If Range("AX" & i) = 1 Then
                Range("Q" & i) = ""
                Range("R" & i) = ""
                Range("S" & i) = ""
            End If
        Next
    End Sub
    Click *, if my suggestion helps you. Have a good day!!

  4. #4
    Registered User
    Join Date
    02-05-2011
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    31

    Re: Macro - Clear cell contents based on other cell

    Thanks guys. They both work but is there any way to make them quicker?

    Of the 2000 or so rows there will be anywhere between 20% and 90% that will need to be cleared so this is taking quite a long time.

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro - Clear cell contents based on other cell

    Try this one

    Sub multiple()
    Dim x, i&, LR&
       Application.ScreenUpdating = 0
     On Error Resume Next
     LR = Cells.Find("*", , , , xlByRows, xlPrevious).Row
            
     For i = LR To 5 Step -1
        If Cells(i, "AX") = 1 Then
            If myrng1 Is Nothing Then
                    Set myrng1 = Range(Cells(i, "Q"), Cells(i, "S"))
                Else
                    Set myrng1 = Union(myrng1, Range(Cells(i, "Q"), Cells(i, "S")))
                End If
            End If
        Next i
            If Not myrng1 Is Nothing Then
                myrng1.ClearContents
            End If
              Application.ScreenUpdating = True
    End Sub

  6. #6
    Registered User
    Join Date
    02-05-2011
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    31

    Re: Macro - Clear cell contents based on other cell

    Quote Originally Posted by AB33 View Post
    Try this one

    Sub multiple()
    Dim x, i&, LR&
       Application.ScreenUpdating = 0
     On Error Resume Next
     LR = Cells.Find("*", , , , xlByRows, xlPrevious).Row
            
     For i = LR To 5 Step -1
        If Cells(i, "AX") = 1 Then
            If myrng1 Is Nothing Then
                    Set myrng1 = Range(Cells(i, "Q"), Cells(i, "S"))
                Else
                    Set myrng1 = Union(myrng1, Range(Cells(i, "Q"), Cells(i, "S")))
                End If
            End If
        Next i
            If Not myrng1 Is Nothing Then
                myrng1.ClearContents
            End If
              Application.ScreenUpdating = True
    End Sub
    Wow, what a hero! Thanks

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro - Clear cell contents based on other cell

    97hills,
    You are welcome!

    Could you please now close (Mark) this thread as solved? Go in to the top right-hand side of this page, choose "Thread Tools" from the menu, then select "solved" from the drop down menu.

  8. #8
    Registered User
    Join Date
    02-05-2011
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    31

    Re: Macro - Clear cell contents based on other cell

    Quote Originally Posted by AB33 View Post
    97hills,
    You are welcome!

    Could you please now close (Mark) this thread as solved? Go in to the top right-hand side of this page, choose "Thread Tools" from the menu, then select "solved" from the drop down menu.
    I'll admit I'm a complete novice when it comes to VBA but I'm usually able to figure it out once I can see the code. This one I don't have a clue how it works! I was hoping to be able to edit it and use it for another purpose but can't figure out what you've done.

    It's pretty similar but instead of clearing whatever is in cells Q:S I just need the value "X" to be written in Q only.

    Again, thanks and I'll make sure I mark the thread as solved

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro - Clear cell contents based on other cell

    97 hills,
    Please do not reply with quote- It has not purpose other than to clutter the space of this page.
    Back to the slow code.

      Sub clear()
    
            Application.ScreenUpdating = 0
            LR = Cells.Find("*", , , , xlByRows, xlPrevious).Row
            
            For i = 5 To LR
                If Cells(i, "AX") = 1 Then Cells(i, "Q") = "x"
            
            Next i
            
            Application.ScreenUpdating = True
    End Sub

  10. #10
    Registered User
    Join Date
    02-05-2011
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    31

    Re: Macro - Clear cell contents based on other cell

    Great, thanks

+ 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