ISERROR Runtime error "1004" issue

jonesgirl0122

New Member
Joined
Jul 13, 2010
Messages
6
I have been trying to get this code to work for 2 hours now. I have to columns of dates/times that I need to find the difference between and put that variable in another column that is formated as time. If there is a blank cell in either of those columns it gives me a #Value error. I'm trying to put a space if it returns that error. I'm new to excel and am trying to write a macro with this code and I keep getting Runtime error 1004. Please help.

Sub hoursdown()

Dim myRange As Range, lngLastRow As Long

Range("M1").Select
ActiveCell.FormulaR1C1 = "HH:MM:SS Time Down"
Range("M2").Select


Range("M2").FormulaR1C1 = "=if(iserror((DATE(MID(RC[-12],7,4),LEFT(RC[-12],2),MID(RC[-12],4,2))+TIMEVALUE(RIGHT(RC[-12],8)))-(DATE(MID(RC[-3],7,4),LEFT(RC[-3],2),MID(RC[-3],4,2))+TIMEVALUE(RIGHT(RC[-3],8)))),"""",(DATE(MID(RC[-12],7,4),LEFT(RC[-12],2),MID(RC[-12],4,2))+TIMEVALUE(RIGHT(RC[-12],8)))-(DATE(MID(RC[-3],7,4),LEFT(RC[-3],2),MID(RC[-3],4,2))+TIMEVALUE(RIGHT(RC[-3],8)))"

LastRow = ActiveSheet.UsedRange.Rows.Count
Range("M2").NumberFormat = "[h]:mm:ss;@"
Range("M2").AutoFill Destination:=Range("M2:M" & LastRow)

End Sub
 

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
Welcome to the board.

You're missing a parenthesis )

In every formula, you must have equal # of ( and )
With the exeption if the ( is part of a text string inside quotes.

I added a ) at the very end.
And it at least compiles, but not sure if it results in the formula you want..

try

Range("M2").FormulaR1C1 = "=if(iserror((DATE(MID(RC[-12],7,4),LEFT(RC[-12],2),MID(RC[-12],4,2))+TIMEVALUE(RIGHT(RC[-12],8)))-(DATE(MID(RC[-3],7,4),LEFT(RC[-3],2),MID(RC[-3],4,2))+TIMEVALUE(RIGHT(RC[-3],8)))),"""",(DATE(MID(RC[-12],7,4),LEFT(RC[-12],2),MID(RC[-12],4,2))+TIMEVALUE(RIGHT(RC[-12],8)))-(DATE(MID(RC[-3],7,4),LEFT(RC[-3],2),MID(RC[-3],4,2))+TIMEVALUE(RIGHT(RC[-3],8))))"


The result is this formula in M2

=IF(ISERROR((DATE(MID(A2,7,4),LEFT(A2,2),MID(A2,4,2))+TIMEVALUE(RIGHT(A2,8)))-(DATE(MID(J2,7,4),LEFT(J2,2),MID(J2,4,2))+TIMEVALUE(RIGHT(J2,8)))),"",(DATE(MID(A2,7,4),LEFT(A2,2),MID(A2,4,2))+TIMEVALUE(RIGHT(A2,8)))-(DATE(MID(J2,7,4),LEFT(J2,2),MID(J2,4,2))+TIMEVALUE(RIGHT(J2,8))))
 

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
Although, I now have a sneeking suspition that:

That whole huge formula can be replaced with

Range("M2").Formula = "=IF(ISERROR(A2-J2),"""",A2-J2)"
 

jonesgirl0122

New Member
Joined
Jul 13, 2010
Messages
6
Thanks so much for quick responses! The reason I had the formula that way is because the dates are in "general" format (from data exported) - and the formula worked fine until I put the if(iserror part. Jonmo, I tried the code you suggested and I didn't get the runtime 1004 anymore but did get a generic box with a red X and just 400 on it. What does this mean?

Thanks!
 

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
OK, start over.

When using vba to enter formulas, I find it best to

First, enter the formula normally by hand.
When it works, THEN convert it to VBA

Can you post a formula that works as intended, that was entered manually by hand ?

Also post sample date/time data from A2 and J2
 
Last edited:

jonesgirl0122

New Member
Joined
Jul 13, 2010
Messages
6
Here it is


Range("M2").FormulaR1C1 = "=(DATE(MID(RC[-12],7,4),LEFT(RC[-12],2),MID(RC[-12],4,2))+TIMEVALUE(RIGHT(RC[-12],8)))-(DATE(MID(RC[-3],7,4),LEFT(RC[-3],2),MID(RC[-3],4,2))+TIMEVALUE(RIGHT(RC[-3],8)))"
 

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
So that code works, now you want it added in an IF(ISERROR structure...

Try

Code:
Range("M2").FormulaR1C1 = "=if(iserror((DATE(MID(RC[-12],7,4),LEFT(RC[-12],2),MID(RC[-12],4,2))+TIMEVALUE(RIGHT(RC[-12],8)))-(DATE(MID(RC[-3],7,4),LEFT(RC[-3],2),MID(RC[-3],4,2))+TIMEVALUE(RIGHT(RC[-3],8)))),"""",(DATE(MID(RC[-12],7,4),LEFT(RC[-12],2),MID(RC[-12],4,2))+TIMEVALUE(RIGHT(RC[-12],8)))-(DATE(MID(RC[-3],7,4),LEFT(RC[-3],2),MID(RC[-3],4,2))+TIMEVALUE(RIGHT(RC[-3],8))))"
 

Forum statistics

Threads
1,082,285
Messages
5,364,287
Members
400,787
Latest member
bs04c

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top