# Column Clearing Coding

#### 3d4d5c

##### New Member
Good Day people,

I was practicing some VBA questions and had hit a roadblock in the coding.
The question was asking me to calculate distance between two points. My Goal was to clear the previously obtained value.
However, the whole excel sheet got cleared.

Here's the coding

If (Range("A2") <> "" and Range("B2" ="") then
Range("A2").clear
ElseIf (Range("A2") <> "" and range("B2" <> "") then
Selection.end(xlToRight).select: CNo=Activecell.column
Range("A2", cells(2, CNo)).clear
End If

Can Someone help me?

### Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi there, first thing is on the first line it should be Range("B2") = "". Same with your second if statement, it needs to be Range("B2") <> "".

What cell did you have selected while running this? Your code is saying if A2 isn't blank and if B2 isn't blank, clear the range of A2 to whatever your last column is through row 2. If you only have data in row 2, that would be why it's all being cleared.

Hi there, first thing is on the first line it should be Range("B2") = "". Same with your second if statement, it needs to be Range("B2") <> "".

What cell did you have selected while running this? Your code is saying if A2 isn't blank and if B2 isn't blank, clear the range of A2 to whatever your last column is through row 2. If you only have data in row 2, that would be why it's all being cleared.

OMG Thank you so much. I did not realise that mistake. Thank You!

No problem, happy I could help.

Replies
5
Views
198
Replies
2
Views
212
Replies
4
Views
336
Replies
14
Views
368
Replies
13
Views
780

1,203,453
Messages
6,055,530
Members
444,794
Latest member
HSAL

### 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.

### Which adblocker are you using?

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

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