|
-
Jan 31st, 2011, 10:43 AM
#1
Thread Starter
New Member
How do I read/write Excel cell data to textbox & visaversa
Hi,
I'm converting older VBA code to VB. I'm using Visual Studio or VB 2010 but am new to this "oop" approach and not a seasoned programmer.
Scenario:
1. Open new Excel worksheet (see below - will this work?)
Public Class frmMain
Prvate Sub frmMain_Load(ByVal sender As System.Object, ByVal e As -System.EventArgs) Handles MyBase.Load
Dim objExcel As New Excel.Application
objExcel.Visible = True
Object.Workbooks.Add()
2. Load text found in cell Range("A1") into form's Textbox1
3. Enable change/edit in Textbox1 to change A1 cell value
Can someone show me a basic example? Assume only one textbox and one cell for above scenario to keep it simple to begin.
Any quick help here would be much appreciated.
Thank you.
-
Jan 31st, 2011, 11:21 AM
#2
Addicted Member
Re: How do I read/write Excel cell data to textbox & visaversa
Well since you're copying data from the worksheet, I'm guessing its not a "new" worksheet. But you have to declare the application, workbook, and worksheet that you are trying to get data from
Code:
Dim excelApp As New Microsoft.Office.Interop.Excel.Application
Dim excelBook As Microsoft.Office.Interop.Excel.Workbook
Dim excelSheet As Microsoft.Office.Interop.Excel.Worksheet
excelBook = excelApp.Workbooks.Open("path")
excelSheet = excelBook.Worksheets(1)
'Now that you have your worksheet you can do your data manipulation
Textbox1.Text = excelSheet.Range("A1").Text
'or to edit
excelSheet.Range("A1").Text = Textbox1.Text
excelBook.Close
excelApp.Close
-
Jan 31st, 2011, 12:28 PM
#3
Thread Starter
New Member
Re: How do I read/write Excel cell data to textbox & visaversa
Thank you.
I actually would like application to add a new workbook to begin, then save, then evoke and edit the same textbox again.
I did a quick test on what you provided earlier that gave me more insight and any more help for above "add and save" would be appreciated again.
-
Jan 31st, 2011, 12:42 PM
#4
Addicted Member
Re: How do I read/write Excel cell data to textbox & visaversa
Well if you want to create a new Workbook, you'd just declare it as a new workbook, add a worksheet, and edit that sheet. To save you'd pass the SaveChanges parameter when you close the workbook
Code:
excelBook2 = new Excel.Workbook
excelsheet = excelBook2.Worksheets(1)
'I think it should already have 3 worksheets, thats why I set the worksheet to the first worksheet in the new workbook.
'Add your changes
Textbox1.Text = excelSheet.Range("A1").Text
excelBook2.Close(SaveChanges:=True)
Depending on what you're doing you can loop it, or still edit the same textbox too
-
Mar 8th, 2012, 08:32 AM
#5
New Member
Re: How do I read/write Excel cell data to textbox & visaversa
Error 1 'Application' is ambiguous in the namespace 'Microsoft.Office.Interop.Excel'.
getting erro while declearing
Dim excelApp As New Microsoft.Office.Interop.Excel.Application
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|