Excel/VBA Solution


I have a code that loops through the cells in a range and determines whether the date values in each cell match the datevalue of a cell in another sheet:
For Each myCell In hiddenRange
 If DateValue(myCell.Value) = DateValue(sh1.Range("AQ12).Value)

I am getting a 'type mismatch error' on the DateValue(myCell.value) portion. Is there another way to write the datevalue of the myCell so that it refers to the date only rather than the contents of the whole cell (in this case, the cells contain dates and times) and I am only looking for a date match.

Thanks for the help,

ANSWER:  Jesse

For Each myCell In hiddenRange

 If Application.Trunc(myCell.Value) = Application.Trunc(sh1.Range("AQ12).Value) Then

this assumes that the values in all the cells are actually stored as dates - not as Strings that look like dates

Tom Ogilvy

---------- FOLLOW-UP ----------

QUESTION: I received an error with Application.Trunc. Would the fact that I am using Excel 2010 have anything to do with the error?

As another option, would this work to breakdown the contents of the cells to match dates?

Format(myCell.value, "m/d/yyyy") ...


For Each myCell In hiddenRange
If int(myCell.Value) = Int(sh1.Range("AQ12).Value) Then

I use Trunc in the worksheet and most worksheet functions are supported in VBA but apparently this one is not.  My mistake.

Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.