Solved

runtime error 1004 in excel vba

Posted on 2003-10-22
9
145,426 Views
Last Modified: 2011-08-18
Would anyone have even the slightest idea what might be going wrong with the following code?
Sometimes it works fine, but most of the time I get a 'runtime 1004: application-defined or object-defined error'

NOTES:
-When stepping through application, runtime 1004 error is raised on the 'End Function' of the parseFormula function

Offending Code
------------------

Chunk of offending code from main function
Sheet1.cells(x, y).Value = Sheet1.cells(x, y).Value & parseFormula(ri(i).getPreFormula, x)  ' first var is string, second is integer

Same as above but I've substituted function calls with the data that's actually being passed
Sheet1.cells(x, y).Value = Sheet1.cells(x, y).Value & parseFormula("1+", 2) ' first var is string, second is integer


Parse Function Formula
---------------------------

' Convert $D to D & row Number
' IE: $D+E$ would return D4+E4 for fourth row, D5+E5 for fifth row, etc...

Public Function parseFormula(ByVal formula As String, ByVal row As Integer) As String
    Dim a As Integer
    Dim char As String
    Dim formattedFormula As String
   
    For a = 1 To Len(formula)
        char = Mid(formula, a, 1)
           
        If char = "$" Then
            If Asc(LCase(Mid(formula, a + 1, 1))) >= 97 And Asc(LCase(Mid(formula, a + 1, 1))) <= 122 Then
                formattedFormula = formattedFormula & Mid(formula, a + 1, 1) & row
                a = a + 1
            End If
        Else
            formattedFormula = formattedFormula & Mid(formula, a, 1)
        End If
    Next a

    MsgBox "." & formattedFormula & "."
    parseFormula = CStr(formattedFormula)
End Function


in debug, parseFormula does indeed return +1, press F8 again and move to End Function line, press F8 again, runtime 1004

The complete application is avaialble if you would like to test it for yourself.
Thanks in advance


Update: I've been working this problem off and on for a few days now and have finally managed to narrow it down a bit.
Seems it doesn't like the
sheet1.cells(x,y).value = sheet1.cells(x,y).value
since msgbox "." & parseFormula(ri(i).getPreFormula, x) & "." works beautifully for all lines and all situations...
0
Comment
Question by:theMuzz
9 Comments
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 9602084
Is your sheet1 dim as excel.worksheets("Sheet1") ?

dragontooth
0
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9603767
Normally you get the error because there is not defined value in Sheet1.cells(x, y).Value. Check value of x & y and make sure they existed. Forexample, Sheet1.cells(0, 0).Value is not existed and will raised the error. Thanks & Cheers.
0
 
LVL 10

Assisted Solution

by:NeoTeq
NeoTeq earned 250 total points
ID: 9605645
Seeming you only get the error at the end of your function, maybe nothing is returned... See below

   For a = 1 To Len(formula)
        char = Mid(formula, a, 1)
           
        If char = "$" Then
            If Asc(LCase(Mid(formula, a + 1, 1))) >= 97 And Asc(LCase(Mid(formula, a + 1, 1))) <= 122 Then
                formattedFormula = formattedFormula & Mid(formula, a + 1, 1) & row
                a = a + 1

!! What if this if-statement is false all the way? Then formattedFormula would be null, and therefore an error would occur in the last line # parseFormula = CStr(formattedFormula) #

            End If
        Else
            formattedFormula = formattedFormula & Mid(formula, a, 1)
        End If
    Next a

just my 2ct

-Neo
0
 

Author Comment

by:theMuzz
ID: 9605931
OK, thanks for the help.

Values of x and y start at 4 and 2 then move onto 4,3 | 4,4 | 4,5 | 5,3 | 5,4 | 5,5 | 6,3 etc...

As for the null return; This has caused me problems on occaision but also seems intermittent.
I've modified my code slightly to msgbox the return from parseFormula instead of printing it to a cell and the applications runs right through without any errors. This leads me to believe the error is in the sheet1.cells(x,y) portion. However, the x and y values are perfectly legitemate values.(as shown above) Now i'm just trying to figure out why sheet1.cells(x,y).value = works in all places but this one...

strange!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Accepted Solution

by:
NeoTeq earned 250 total points
ID: 9606004
Ok in that case...

Instead of using:
 - sheet1.cells(x,y).value
try using:
 - sheet1.cells(x, y).formulaR1C1.

By the way, if the cell doesn't contain a formula already, but a value, you will need to use:
 - sheet1.cells(x, y).formulaR1C1 = "=" & sheet1.cells(x, y).formuR1C1 & parseFormula(ri(i).getPreFormula, x)

That should work...

-Neo
0
 

Author Comment

by:theMuzz
ID: 9606187
still doesn't work...

Is it possible that excel simply doesn't like the formula it's being returned?
=PIExpDat("timelt('033-bleach-downtime','*','-60m',10)/60","10/13/2003 6:00:00 AM","10/13/2003 6:59:00 AM","1h", 0,"name_of_our_piServer")

This is a perfectly valid function call so long as we are using OSI's pipc add-in. I've pasted the above into a blank excel sheet and it does work. (sort of... piServer returns calcFailed but that's a different story, and an easy one to fix at that)
0
 
LVL 10

Expert Comment

by:NeoTeq
ID: 9606806
Thanks for the points... did I miss something? Or did either of us make a typo somewhere?

Ciao!

-Neo
0
 

Author Comment

by:theMuzz
ID: 9606845
no, didn't miss much.

first problem was null return on parseFormula function
second problem as using sheet1.cells(x,y).value instead of .formulaR1C1
third problem, under obscure circumstances, the final formula returned didn't have quotes around the dates.

so,
=PIExpDat("timelt('033-bleach-downtime','*','-60m',10)/60","10/13/2003 6:00:00 AM","10/13/2003 6:59:00 AM","1h", 0,"name_of_our_piServer")
actually came back as
=PIExpDat("timelt('033-bleach-downtime','*','-60m',10)/60",10/13/2003 6:00:00 AM,10/13/2003 6:59:00 AM,"1h", 0,"name_of_our_piServer")
which excel apparently doesn't like.

I consider the question solve with the formulaR1C1 answer since the date problems was not related to the original question.

Thanx a million everyone.
Experts Exchange save me again!
0
 

Expert Comment

by:aftabkashif
ID: 14919603
You guys are doing a great job. Thanks for helping me out!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
firstChar challenge 13 107
oracle query help 18 99
creating threads in delphi 1 82
How to get time difference in minutes and seconds only between 2 dates 2 20
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now