Column Clearing Coding

3d4d5c

New Member
Joined
Feb 8, 2019
Messages
10
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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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.
 
Upvote 0
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!
 
Upvote 0

Forum statistics

Threads
1,214,537
Messages
6,120,096
Members
448,944
Latest member
SarahSomethingExcel100

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