vba help - cdate

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I am trying to convert a text Range into Date using CDate. but it converts single cell only.
Range("d1:d17").Value = CDate(Range("d1:d17").Value) ... type mismatch any alternate.

Option Explicit

Sub ConvertTextNumber_ToDate()

Range("d1").Value = CDate(Range("D1").Value)

Range("d1:d17").Value = CDate(Range("d1:d17").Value) ... type mismatch


End Sub

Below is a Table.

Book2
DE
104/08/2012TRUE
208/04/2012FALSE
308/04/2012FALSE
408/04/2012FALSE
508/04/2012FALSE
608/04/2012FALSE
708/04/2012FALSE
808/04/2012FALSE
908/04/2012FALSE
1008/04/2012FALSE
1108/04/2012FALSE
1208/04/2012FALSE
1308/04/2012FALSE
1408/04/2012FALSE
1508/04/2012FALSE
1608/04/2012FALSE
1708/04/2012FALSE
Sheet1
Cell Formulas
RangeFormula
E1:E17E1=ISNUMBER(D1)



Thanks
mg
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try:

Range("D1:D17").Value = Evaluate("IF({1},D1:D17+0)")

You'll probably want to format the cells as a date.
 
Upvote 0
Hi Eric

Thats great ! it worked,

how to read this line= Evaluate("IF({1},D1:D17+0)") or how it works

Thanks
mg
 
Upvote 0
Evaluate is a VBA function that will evaluate an Excel formula. One feature of it is that it handles array formulas by default. So for an IF 0 means False and 1 means True, and you put the 1 in array braces to say it's an array formula, then the True result of the formula is D1:D17+0. If you do arithmetic (+0) on a number formatted as text, it converts it to a number first. Then since it's an array formula, it works on all the values of D1:D17, returns an array, which is then sent to the D1:D17 range.

It's kind of a tricky formulation. It's a 1-liner, and it works, but there is something to be said about a more "standard" VBA way of doing it, like:

VBA Code:
    For Each c In Range("D1:D17")
        c.Value = CDate(c.Value)
    Next c

But it's up to you.
 
Upvote 0
Hi Eric,

Thank you once again for your help and also explaining logic behind Evaluate. ?


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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