Search code examples
vb.netexcelworksheet-function

Writing Excel formula using vb.net


I want to write a formula to SUM from column B4 to M4, this should be done using vb.net programming.

I tried using the following stuff:

oXLWsheet.Range(4, 14).Formula = "=SUM(oXLWsheet!$B$4:M$4)" 
                                 "=SUM(B4:M4)" 
                                 "=SUM(B4,C4,D4,E4,F4,G4,H4,I4,J4,K4,L4,M4)"

Nothing is working for me. I'm getting the following error when I run the code:

"Exception from HRESULT: 0x800A03EC".


Solution

  • Try quoting the range, and if you're trying to fill two different rows you have to show each row like so:

    oXLWsheet.Range("4:4,14:14").Formula = "=SUM(oXLWsheet!$B$4:M$4)"
    

    This of course gives you a circular reference. If you're just trying to put the values in cell N4, then use this:

    oXLWsheet.Range("$N$4").Formula = "=SUM(oXLWsheet!$B$4:M$4)"
    

    (Dollar signs optional)