VBA text functions

theta

Well-known Member
Joined
Jun 9, 2009
Messages
960
Hi...I have a named ranged on my sheet that is called "UserTrafficPeriod"

It is a number cell but formatted "0000" - so if I enter 311, it is displayed as 0311 (meaning MARCH 2011).

This is a traffic period, and must be entered in this format.

Now in VBA I need to compare this traffic period to another cell.

To compare them directly I need to flip the year / date to ensure the <= compare works. But not sure how to do it in VBA. In formula I would do it like this :

=RIGHT(TEXT(UserTrafficPeriod,"0000"),2)&LEFT(TEXT(UserTrafficPeriod,"0000"),2)

Would give me the (logically correct) result of 1103 (without this I would be looking at 311 which is less than 1210, although this would not produce the desired result as they represent period)

But not sure on the VBA equivalent. Then could do a simple range("a1").value <= [VBA text bit goes here]

Any help appreciated!
 

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.
Could you give me a VBA example...just so that i'm crystal clear.

The data set I work on is quite large so would take 5 minutes before the error even appeared (each time I run the code)

Rich (BB code):
If sPMN = ThisWorkbook.Sheets("PMN").Cells(r, 1) AND ThisWorkbook.Sheets("PMN").Cells(r, 4) <= [format logic] Then
 
Upvote 0
Something like this:
ThisWorkbook.Sheets("PMN").Cells(r, 4) <= Right(Format(Range("UserTrafficPeriod"),"0000")+0,2)
 
Upvote 0
Personally I would compare date values as date values, and such I would convert both values to a date. E.g:
Code:
datevalue(format$("01" & range("a1").Text,"00\/00\/00"))
 
Upvote 0
Hi Jon...so what would be the best way to convert [m]myy into a true date

Inputs would be like :

910
1010
1110
1210
111
211
311

etc
 
Upvote 0
Well I assumed that the numbers were formatted as "0000", rather than as date (which is why I was using the Text property).

Code:
datevalue(format$("01" & range("a1").Text,"00\/00\/00"))</pre>

However if those are actually dates, formatted as [m]myy then you can compare the date values directly by only using the Value property.
 
Upvote 0
Thanks. Solution works. But bit long winded, any way of shortening? Didn't see the need to define the named ranges as variables as I only use them once in the whole macro

Code:
If sPMN = ThisWorkbook.Sheets("PMN").Cells(r, 1) And ThisWorkbook.Sheets("PMN").Cells(r, 3) = ThisWorkbook.Sheets("INDEX").Range("UserHub") And ThisWorkbook.Sheets("PMN").Cells(r, 4) <= DateValue(Format$("01" & ThisWorkbook.Sheets("INDEX").Range("UserTrafficPeriod").Text, "00\/00\/00")) Then

Works perfectly though
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,833
Members
452,947
Latest member
Gerry_F

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