Why does if activecell.value = range("S1") work only sometimes?

nick1408

Board Regular
Joined
Jan 18, 2010
Messages
82
I have the following code:

EqiYq4c.png


As you can see from the immediate window both cell values look the same but the code decides it isn't and skips to the 'Else' comment. Why would this be? I would expect the very next line to be completed in this instance. I have also tried using .text and get the same result from the code.

When the code looks for something that doesn't have a space (i.e. 'Cage1') it works. What is the best way to get the code to not care about the space?

Thanks!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
You need to copy the code and paste it in this forum like you have seen thousands of other poster do.
 
Upvote 0
When the code looks for something that doesn't have a space (i.e. 'Cage1') it works. What is the best way to get the code to not care about the space?

You can use the Trim function to get rid of leading and trailing spaces. As an example
Code:
If Trim(Range("A1").Value) = Trimg(Range("B1").Value) Then
     'Do something
End If
 
Upvote 0
You can use the Trim function to get rid of leading and trailing spaces. As an example
Code:
If Trim(Range("A1").Value) = Trimg(Range("B1").Value) Then
     'Do something
End If

Thanks mate. It's actually the middle space that the activecell.value doesn't like. I'm not sure how to get the cell to ask if it is a match or not now.
 
Upvote 0
Check to make sure it really is a space !!
Is the data downloaded from another source ??
If so, it may be a NBSP not a whitespace.
Have a look here for code to TRIM all excess characters


Rearranging Data in Columns
 
Upvote 0
You could remove all spaces using Replace.
Code:
If Replace(ActiveCell.Value, " ", "") = Replace(Range("B1").Value, " ", "") Then
     'Do something
End If
 
Upvote 0

Forum statistics

Threads
1,214,393
Messages
6,119,261
Members
448,880
Latest member
aveternik

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