If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > assigning a string variabel to a cell formular

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-27-11, 05:58
Trinsan Trinsan is offline
Registered User
 
Join Date: Oct 2005
Posts: 168
assigning a string variabel to a cell formular

I've been searching the forums for a while, finding similar problems, but none solving my exact situation.

Observe the below code snippet, and focus on the string variabel formeltekst, which I would later like to the formula of a specific cell: a.offset(0, taloffset).

But I get an error 1004.

An example of how the formeltekst string would look "=2731,21+2731,23+897,95+975,25".

its a user request that the formula is visible, which is why I dropped and commented out the verdi variable that would otherwise just add the sum/value of the formula instead.

Any suggestions? (apart from shooting the user demanding to be able to see the formula?)

Cheers, Trin

Code:
    For Each a In Selection

        kommando.Parameters.Item(0) = aarstal
        kommando.Parameters.Item(1) = a.Value
        Set GIDrs = kommando.Execute
        
        While GIDrs.EOF <> True
                                
            verdi = verdi + GIDrs![Beløb (DKK)]
            tekst = tekst & GIDrs![Beløb (DKK)] & " Kr som forfaldt: " & Format(GIDrs![Forfaldsdato], "mmm, yyyy") & vbNewLine
            formeltekst = formeltekst & "" & GIDrs![Beløb (DKK)] & "+"
            GIDrs.MoveNext
        
        Wend
        
        formeltekst = Left(formeltekst, Len(formeltekst) - 1)
        formeltekst = "=" & formeltekst
        
        a.offset(0, taloffset).Formula = formeltekst
        'a.offset(0, taloffset).Value = verdi
        a.offset(0, taloffset).AddComment
        a.offset(0, taloffset).Comment.Visible = False
        a.offset(0, taloffset).Comment.Text Text:="" & tekst & ""
        a.offset(0, taloffset).Comment.Shape.TextFrame.AutoSize = True
            
        verdi = 0
        tekst = ""
        formeltekst = ""
        
        Set GIDrs = Nothing
    
    Next a
__________________
IT squid: networks, servers, firewalls, routers and I dabble a little with SQL-server and Access as well....
Reply With Quote
  #2 (permalink)  
Old 09-27-11, 16:51
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Hi Trin,

Which line throws the error and what is the error number/message?

I tested your formula string like this ( "." rather than "," is the decimal separator in my locale) :
Code:
Sub foo()
    Const strFORMULA As String = "=2731.21+2731.23+897.95+975.25"
 
    Range("A1").Formula = strFORMULA
 
End Sub
And it worked fine.
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On