none
VBA code to clear contents of cell C if cells A and B are blank RRS feed

  • Question

  • Hello! Working on a code that will clear contents of cell C if cells A and B are 
    blank. (It will not delete entire row.) The below code is giving me an error of: 
    Invalid procedure or call argument 
    Appreciate your help!  

    Dim g As Double
        For g = 2 To ActiveSheet.UsedRange.Rows.Count
            If (CELLS("A" & "B" & g) = "") Then
                   Cells(g, "C").ClearContents
                  
            End If
        Next
    

    Thursday, March 29, 2018 10:14 PM

Answers

  • Jackie,

    Dim g As Long
        For g = 2 To ActiveSheet.UsedRange.Rows.Count
        If Cells(g, "A").Value = "" And Cells(g, "B").Value = "" Then
            Cells(g, "C").ClearContents             
        End If
        Next
    '---
    Jim Cone
    https://goo.gl/IUQUN2
    Friday, March 30, 2018 12:07 AM
  • Hi IamJackie,

    You can also try to use Range to achieve the same output.

    Modified Code:

    Sub demo()
    Dim g As Double
        For g = 2 To ActiveSheet.UsedRange.Rows.Count
            If ActiveSheet.Range("A" & g) = "" And ActiveSheet.Range("B" & g) = "" Then
                   Cells(g, "C").ClearContents
                  
            End If
        Next
    End Sub
    

    Output:

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 30, 2018 1:42 AM
    Moderator

All replies

  • Jackie,

    Dim g As Long
        For g = 2 To ActiveSheet.UsedRange.Rows.Count
        If Cells(g, "A").Value = "" And Cells(g, "B").Value = "" Then
            Cells(g, "C").ClearContents             
        End If
        Next
    '---
    Jim Cone
    https://goo.gl/IUQUN2
    Friday, March 30, 2018 12:07 AM
  • Hi IamJackie,

    You can also try to use Range to achieve the same output.

    Modified Code:

    Sub demo()
    Dim g As Double
        For g = 2 To ActiveSheet.UsedRange.Rows.Count
            If ActiveSheet.Range("A" & g) = "" And ActiveSheet.Range("B" & g) = "" Then
                   Cells(g, "C").ClearContents
                  
            End If
        Next
    End Sub
    

    Output:

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 30, 2018 1:42 AM
    Moderator
  • Thank you very much! The code worked to perfection! :D
    Friday, March 30, 2018 2:23 PM
  • Thank you very much! The code worked to perfection! :D
    Friday, March 30, 2018 2:23 PM