Piccinin
Piccinin

Reputation: 103

Difference between the following lines of code

I am writing a long macro which is essentially copying and pasting a bunch of stuff. However, i need some help in deciphering what is the difference in the following lines of code:

Something.Offset(0, -87) Like "-"

Something.Offset(0, -87)= "-"

Something.Offset(0, -87).Value= "-"

Something.Offset(0, -87) Like "*-*"

This is for those items which only have “-“ as their data.

Upvotes: 1

Views: 76

Answers (2)

velblúd
velblúd

Reputation: 363

Short answer: In case that the cell contains always "-", they are the same.

Long answer:

1st line uses Like operator to determine whether the cell's value matches pattern "-", which matches only the string "-".

2nd line is identical to 3rd line, because .value is a default getter of Range.

3rd line gets the value of cell and compare it with string "-" (that should be the prefered method if you do not need functionality of Like).

4st line uses Like operator to determine whether the cell's value matches pattern "*-*". * is a wildcard to match 0 or more characters. It is valid not only for "-", but also "ABC-EFG", "A-", "-A" and so on.

More about Like operator https://analystcave.com/vba-like-operator/

Upvotes: 4

Jchang43
Jchang43

Reputation: 891

So .Value is the implicit property you call when you don't specify after a range so between the center two there should be no difference.

Between the center two and the last one, the center ones will check to see if the value is exactly equal to a minus sign, but the bottom one will check to see if the cell contains a minus sign. The wildcards * allow there to be anything on the side that it's specified on, so by surrounding both, you're allowing it to appear anywhere in the cell's value. Essentially Like allows you to input a pattern by using different symbols which you can find here. It's also worth noting that like is typically used for strings and = is typically used for numbers.

The top one will function the same as the center two because you haven't added any pattern operators in the comparison string like you did in the last one.

At least this is my understanding of the differences.

Upvotes: 2

Related Questions