Question How to assign Excle cell value to textbox

benmasoud

New member
Joined
Nov 5, 2011
Messages
1
Programming Experience
3-5
Hi there, i want to assign excle cell value to textbox in event SheetSelectionChange but every time when the program reach the line textbox1 it goes again the form without assign the cell value!!!


Private Sub xWorkbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range) Handles xWorkbook.SheetSelectionChange


Dim sheet As Excel.Worksheet = CType(Sh, Excel.Worksheet)
Me.TextBox1.Text = sheet.Name & ":" & _
Target.Address(ReferenceStyle:=Excel.XlReferenceStyle.xlA1)


End Sub
 
Are you using VSTO or are you writing your own code for the workbook?

If you're writing your own then

VB.NET:
Imports Excel = Microsoft.Office.Interop.Excel
Dim xlApp As Excel.Application = Nothing
Dim xlWkbNew As Excel.Workbook = Nothing
Dim xlWkbExisting As Excel.Workbook = Nothing
Dim xlWkshtMain As Excel.Worksheet = Nothing
'Instantiate a new Excel session.
xlApp = New Excel.Application
'Add a new workbook.
xlWkbNew = xlApp.Workbooks.Add
'Or if you're opening an already existing workbook
xlWkbExisting = xlApp.Workbooks.Open(path.xlsx)

'Existing WorkSheet Reference the Sheet you want the values from
xlWorkSheet = CType(xlWorkBook.Worksheets("Sheet Name"), Worksheet)
'Reference the first worksheet in the new workbook.
xlWkshtMain = CType(xlWkbNew.Worksheets(Index:=1), Excel.Worksheet)
'Then you can reference the cell value like this
Dim txt As String = xlWkshtMain.Cells(3, 2).ToString
'Or
TextBox1.Text = CStr(xlWkshtMain.Cells(3, 2))

If it's VSTO maybe someone else on here can help you. I have never had much success with VSTO. It seems like it's very restrictive. But that's just my opinion..
 
Back
Top