Clear Cells based on User Input Box to define Row Number

Lisa W

New Member
Joined
Sep 28, 2021
Messages
5
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hi I have the code below which works well, however I need it to be based on both columns C & J. So if "C" is empty / blank then Clear Contents of "J". I have tried several ways to add "J" into the range without success. Any help would be greatly appreciated:

Public Sub RAG_Click()
'To Clear RAG Status and re-enter'
Dim x As Integer
ActiveSheet.Unprotect
x = InputBox("Please Enter the Row Number")
Range("C" & x).ClearContents
ActiveSheet.Unprotect
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Welcome to the Board!

If you want to clear column J if column C is blank, then change this line:
VBA Code:
Range("C" & x).ClearContents
to this:
VBA Code:
If Range("C" & x) = "" Then Range("J" & x).ClearContents
 
Upvote 0
Thanks Joe, this is working for Column J but now not clearing Column C if not empty/blank. The Values for both of these cells are Text, controlled by Data Validation if that makes a difference. (R, A, G, U)?
 
Upvote 0
It is not quite clear what you want.

Your original code was clearing column C.
But then you said clear column J IF column C is blank.
So, do you want to check column C or not?
If you clear column C, of course it will be blank.

If you want to clear BOTH columns C and J, and not bother checking them first, then just add another line to your code like the first, i.e.
Rich (BB code):
Public Sub RAG_Click()
'To Clear RAG Status and re-enter'
Dim x As Integer
ActiveSheet.Unprotect
x = InputBox("Please Enter the Row Number")
Range("C" & x).ClearContents
Range("J" & x).ClearContents
ActiveSheet.Unprotect
End Sub
 
Upvote 0
Solution
Hi,
try

VBA Code:
Public Sub RAG_Click()
    'To Clear RAG Status and re-enter'
    Dim x           As Variant
    ActiveSheet.Unprotect
   
    Do
        x = InputBox("Please Enter the Row Number", "Enter Row")
    Loop Until IsNumeric(x)
    'cancel pressed
    If StrPtr(x) = 0 Then Exit Sub
   
    With Cells(CLng(x), "C")
          .ClearContents
         .Offset(, 7).ClearContents
    End With
   
    ActiveSheet.Unprotect
End Sub

Dave
 
Last edited:
Upvote 0
Sorry Joe I wasn't very clear reading it back so either Column C or Column J would contain text not both on same row.
 
Upvote 0
Does the last code I posted do what you want?
If not, please explain how it is not doing what you want.
 
Upvote 0
Thanks Joe, yes it works not sure why it didn't when I tried that prior to posting. Thank you very much for your help.
 
Upvote 0
You are welcome.
Note that when marking the solution, you want to mark the post that actually contains the solution.
I have re-marked it for you, as the post of mine that you marked did not have the solution (code) in it.
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,078
Latest member
skydd

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top