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

nick1408

Board Regular
Joined
Jan 18, 2010
Messages
80
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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
19,343
Office Version
  1. 2021
Platform
  1. Windows
You need to copy the code and paste it in this forum like you have seen thousands of other poster do.
 
Upvote 0

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
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

nick1408

Board Regular
Joined
Jan 18, 2010
Messages
80
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

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,591
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
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

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
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,190,917
Messages
5,983,576
Members
439,852
Latest member
balasat

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
Top