Hello
Can someone show me how to read a value from a cell in Excel?
Example:
Cell A1 has the text "TRUE"......how do I read this value back?
I am using Excel Interop...so far I have tried these statements, but with no success
excelapp.range["A1:A1"].value
excelapp.range["A1:A1"].ToString()
thanks
J
Loading
Altaf PatelPosted Feb 24, 2010, 3:05 AM
add references:
Microsoft Excel 11.0 Object library
Dim xlapp As Excel.Application
Dim Wb As Excel.Workbook
Dim Ws As Excel.Worksheet
Dim var As Variant
Dim i As Integer
Dim Last_id As Integer
Private Sub cmd_delete_Click()
Call delete
cmd_next.SetFocus
Call Nxt
End Sub
Private Sub Form_Load()
Form1.Caption = Date
Set xlapp = New Excel.Application
Set Wb = xlapp.Workbooks.Open(App.Path & "\TestCreate.xls")
Set Ws = Wb.Worksheets("Sheet1")
Cmd_Add.Enabled = False
Last_id = getlast
Form1.Show
i = 1
Call first
'************Label ******************
'Ws.Range("A1:B1").MergeCells = True
ActiveCell.Borders(xlEdgeLeft).Color = RGB(255, 10, 10)
Ws.Range("A1").Borders.Color = RGB(255, 165, 160)
Ws.Range("A1").Font.Color = vbBlue
Ws.Range("A1").Font.Bold = True
Ws.Cells(1, 1).Value = "Name"
Ws.Range("B1").Font.Bold = True
Ws.Range("B1").Font.Color = vbBlue
Ws.Range("B1").Value = "Contact"
Ws.Range("C1").Value = "Email"
Ws.Range("C1").Font.Bold = True
Ws.Range("C1").Font.Color = vbBlue
Ws.Range("D1").Value = "Mont. Sal"
Ws.Range("D1").Font.Bold = True
Ws.Range("D1").Font.Color = vbBlue
Ws.Range("E1").Value = "OT"
Ws.Range("E1").Font.Bold = True
Ws.Range("E1").Font.Color = vbBlue
Ws.Range("A1:F1").Borders.Weight = xlThick
Ws.Range("F1").Font.Bold = True
Ws.Range("F1").Font.Color = vbRed
Ws.Range("F1").Value = "Total sal"
Ws.Range("F1").Font.Color = vbRed
txt_TS.Text = Val(txt_MS.Text) + Val(txt_ot.Text)
' ***** To get sum of all salary *****
Ws.Cells(4, 8).Value = "Sal"
Ws.Cells(4, 8).Font.Color = vbBlack
Ws.Cells(5, 8).Formula = "= sum(F2:F20)"
End Sub
Private Sub Form_Unload(Cancel As Integer)
Wb.Save
xlapp.ActiveWorkbook.Save
xlapp.Quit
End Sub
Private Sub cmd_exit_Click()
xlapp.Quit
End
End Sub
Private Sub cmd_first_Click()
Call first
End Sub
Private Sub cmd_last_Click()
Call last
End Sub
Private Sub cmd_next_Click()
Call Nxt
End Sub
Private Sub cmd_prev_Click()
Call prev
End Sub
Private Sub cmd_update_Click()
Call update
End Sub
Private Sub Cmd_add_Click()
Call Add
cmd_first.Enabled = True
cmd_last.Enabled = True
Cmd_new.Enabled = True
cmd_next.Enabled = True
cmd_prev.Enabled = True
cmd_update.Enabled = True
End Sub
Private Sub Cmd_new_Click()
Cmd_Add.Enabled = True
txt_name.Text = ""
txt_cont.Text = ""
txt_email.Text = ""
txt_MS.Text = ""
txt_ot.Text = ""
txt_TS.Text = ""
cmd_first.Enabled = False
cmd_last.Enabled = False
Cmd_new.Enabled = False
cmd_next.Enabled = False
cmd_prev.Enabled = False
cmd_update.Enabled = False
End Sub
Public Function Add()
Dim name As String
Dim cont As String
Dim email As String
name = txt_name.Text
cont = txt_cont.Text
email = txt_email.Text
mont_sal = txt_MS.Text
ot = txt_ot.Text
'Last_id = getlast + 1
Last_id = Last_id + 1
If txt_name.Text = "" Then
MsgBox ("enter name")
txt_name.SetFocus
ElseIf txt_cont.Text = "" Then
MsgBox ("enter contact")
txt_cont.SetFocus
ElseIf txt_email.Text = "" Then
MsgBox ("enter email address")
txt_email.SetFocus
ElseIf txt_MS.Text = "" Then
MsgBox ("enter Monthly salary")
txt_MS.SetFocus
ElseIf txt_ot.Text = "" Then
MsgBox ("enter OT amount")
txt_ot.SetFocus
End If
Cmd_Add.Enabled = True
txt_TS.Text = Val(txt_MS.Text) + Val(txt_ot.Text)
i = Last_id
i = i + 1
Ws.Cells(i, 1).Value = name
Ws.Cells(i, 2).Value = cont
Ws.Cells(i, 3).Value = email
Ws.Cells(i, 4).Value = mont_sal
Ws.Cells(i, 5).Value = ot
'Ws.Cells(getlast + 1, 6).Formula = "= sum(f2:f67)"
Ws.Cells(i, 6).Value = Ws.Cells(i, 4).Value + Ws.Cells(i, 5).Value
Wb.Save
xlapp.ActiveWorkbook.Save
Last_id = i
MsgBox ("Added Succesfully")
' txt_name.Text = ""
' txt_cont.Text = ""
' txt_email.Text = ""
' txt_MS.Text = ""
' txt_ot.Text = ""
' txt_TS.Text = ""
'Call first
Exit Function
End Function
Public Function first()
i = 2
txt_name.Text = Ws.Cells(i, 1).Value
txt_cont.Text = Ws.Cells(i, 2).Value
txt_email.Text = Ws.Cells(i, 3).Value
txt_MS.Text = Ws.Cells(i, 4).Value
txt_ot.Text = Ws.Cells(i, 5).Value
txt_TS.Text = Ws.Cells(i, 6).Value
MsgBox ("first record")
End Function
Public Function last()
'Last_id = getlast
txt_name.Text = Ws.Cells(Last_id, 1).Value
txt_cont.Text = Ws.Cells(Last_id, 2).Value
txt_email.Text = Ws.Cells(Last_id, 3).Value
txt_MS.Text = Ws.Cells(Last_id, 4).Value
txt_ot.Text = Ws.Cells(Last_id, 5).Value
txt_TS.Text = Ws.Cells(Last_id, 6).Value
MsgBox ("last record")
i = Last_id
End Function
Public Function Nxt()
i = i + 1
txt_name.Text = Ws.Cells(i, 1).Value
txt_cont.Text = Ws.Cells(i, 2).Value
txt_email.Text = Ws.Cells(i, 3).Value
txt_MS.Text = Ws.Cells(i, 4).Value
txt_ot.Text = Ws.Cells(i, 5).Value
txt_TS.Text = Ws.Cells(i, 6).Value
If txt_name.Text = "" Then
If txt_cont.Text = "" Then
If txt_email.Text = "" Then
If txt_MS.Text = "" Then
If txt_ot.Text = "" Then
i = i - 1
txt_name.Text = Ws.Cells(i, 1).Value
txt_cont.Text = Ws.Cells(i, 2).Value
txt_email.Text = Ws.Cells(i, 3).Value
txt_MS.Text = Ws.Cells(i, 4).Value
txt_ot.Text = Ws.Cells(i, 5).Value
txt_TS.Text = Ws.Cells(i, 6).Value
MsgBox ("Last Record")
Last_id = i
Exit Function
End If
End If
End If
End If
End If
End Function
Public Function prev()
i = i - 1
If i > 1 Then
txt_name.Text = Ws.Cells(i, 1).Value
txt_cont.Text = Ws.Cells(i, 2).Value
txt_email.Text = Ws.Cells(i, 3).Value
txt_MS.Text = Ws.Cells(i, 4).Value
txt_ot.Text = Ws.Cells(i, 5).Value
txt_TS.Text = Ws.Cells(i, 6).Value
Else
i = 1
Call first
End If
End Function
Public Function delete()
Ws.Rows(i).EntireRow.delete
'ActiveCell.EntireRow.delete
Wb.Save
xlapp.ActiveWorkbook.Save
End Function
Public Function update()
Ws.Cells(i, 1).Value = txt_name.Text
Ws.Cells(i, 2).Value = txt_cont.Text
Ws.Cells(i, 3).Value = txt_email.Text
Ws.Cells(i, 4).Value = txt_MS.Text
Ws.Cells(i, 5).Value = txt_ot.Text
Ws.Cells(i, 6).Value = Val(txt_MS.Text) + Val(txt_ot.Text)
Ws.Cells(i, 6).Font.Color = vbRed
txt_TS.Text = Ws.Cells(i, 6).Value
MsgBox ("Updated ") & txt_name.Text
Wb.Save
xlapp.ActiveWorkbook.Save
End Function
Public Function getlast()
Dim a As Integer
a = 2
For a = 2 To 10000
If txt_name.Text = "" Then
Exit For
If txt_cont.Text = "" Then
If txt_email.Text = "" Then
If txt_MS.Text = "" Then
If txt_ot.Text = "" Then
Exit For
End If
End If
End If
a = a - 1
End If
End If
Next
getlast = a
End Function
please mark this answer if you find it usefull
& let me know your reply.
[email protected]