Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am trying to envoke the Sum function on a range in VB .NET. I am getting an exception: Object Reference not set to an instance of an object.

 

here is my class:

 

Public Class generalFormatFunctions

 

Private ws As Excel.Worksheet

 

Sub New(ByVal worksheet As Excel.Worksheet)

ws = worksheet

End Sub

 

Public Function DeleteInvalidRows()

Dim colNumStart As Integer = 2

Dim wsFuncs As Excel.WorksheetFunction

 

'Delete all rows that have 0 sum - assuming numbers start in col 2

Dim lastCol = ws.UsedRange.Columns.Count

Dim i As Integer

For i = ws.UsedRange.Rows.Count To 1 Step -1

 

If wsFuncs.Sum(ws.Range(ws.Cells(i, colNumStart), ws.Cells(i, lastCol))) = 0 Then

ws.Cells(i, 1).EntireRow.Delete()

End If

 

Next i

End Function

End Class

 

another class invokes this class and passes in a worksheet object. The exception occurs in this line: If wsFuncs.Sum(ws.Range(ws.Cells(i, colNumStart), ws.Cells(i, lastCol))) = 0 Then

 

Any ideas

 

thanks brazilnut

  • Moderators
Posted

you need to declare an Application...


Dim xApp As Excel.Application
xApp = CType(CreateObject("Excel.Application"), Excel.Application)
Dim wsFuncs As Double '(maybe)

wsFuncs = xApp.WorksheetFunction. 'Continue your function here

Visit...Bassic Software
Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...