what i am trying to do is make a program to read an excel file that i already have , choose certin columns depending on its header title then paste these columns into sheet2 .
the problem is when i check for the columns i want to extract it gives me this error "Object reference not set to an instance of an object".

this is the code i am using to extract the value of a certin cell while looping throw a row and if the value of the cell = "Sample" it changes the value of another cell with "Pass"..

Imports Excel = Microsoft.Office.Interop.Excel
Imports System.IO

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartTesting.Click

    Dim oXL As Excel.Application
    Dim oWB As Excel.Workbook
    Dim oSheet As Excel.Worksheet
' start excel application 
     oXL = New Excel.Application
     oXL.Visible = True

        ' Get a new workbook.
        oWB = oXL.Workbooks.Open(C:\filepath.xls)
        oSheet = oWB.ActiveSheet
' just to show me the value of the cell
        MsgBox(oSheet.Cells(1, 1).Value.ToString, MsgBoxStyle.OkOnly)

' starting the loop
        Dim i As Integer = 1
        For i = 1 To 128
            If oSheet.Cells(1, i).Value.ToString = "Sample" Then
                oSheet.Cells(2, 1) = "Pass"
            End If
        Next i

    End Sub

but it does not work :(

thank you for helping

Recommended Answers

All 3 Replies

I'm not sure myself, but maybe this helps


thank you for your help Rapture that really helped me :)

and yesterday i solved my problem with removing the ".tostring"
for ex

' as you can see you dont state the ".value "nor ".tostring"  after" 
' then" 
if osheet.cell(1,i).value = "Sample" then 
osheet.cell(2,i)= "Pass"
end if 
next i

thanks again

I have a graph in my excel file. There is two values that changes this graph appearance, so I want to change these cell values in a FOR loop, in order to make a "animation". Is it work? Every step of the loop will change the graph, but how can I be abke to see it changing? Anyone can help mein this doubt?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.