VB.Net MS Excel Question

S

SHANE CLARK

I have a question...I am attempting to write the values of multiple textboxes to an MS Excel spreadsheet. I would like to be able to write the values to let say a1,b1,c1,d1...ect and then click on a button to write new values to the next row a2,b2,c2,d1...ect...This is what I came up with but it does not do what I need it to do. here is what I am missing.

a.. I would rather write values to a prexisting MS Excell sheet ex "C:/temp/textexcell.xls"
b.. when I click on the next button it would use either the code I have or something to prepare the user to write values to the next row.
Thanks in advance for your help!!!
Public Class Form1

Inherits System.Windows.Forms.Form

Dim alpha As Char

Dim beta As Char

Dim charlie As Char

Dim num As Integer

Dim a As String

Dim b As String

Dim c As String

Dim exsell As New Excel.Application



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

num = 1

alpha = "a"

beta = "b"

Charlie ="c"



a = Convert.ToString(alpha & num)

b = Convert.ToString(beta & num)

c = Convert.ToString(Charlie & num)

Dim exsell As New Excel.Application


exsell.Visible = True

exsell.Workbooks.Add()

exsell.Range(a).Select()

exsell.ActiveCell.FormulaR1C1 = TextBox1.Text

exsell.Range(b).Select()

exsell.ActiveCell.FormulaR1C1 = TextBox2.Text

textbox1.text =""

textbox2.text =""

textbox3.text =""







End Sub



'Private Sub btnNextRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

' num = num + 1

' alpha = "a"

' beta = "b"

' Charlie ="c"

' a = Convert.ToString(alpha & num)

' b = Convert.ToString(beta & num)

' c = Convert.ToString(charlie & num)

exsell.Range(a).Select()

exsell.ActiveCell.FormulaR1C1 = TextBox1.Text

exsell.Range(b).Select()

exsell.ActiveCell.FormulaR1C1 = TextBox2.Text

' exsell.Range(c).Select()

' exsell.ActiveCell.FormulaR1C1 = TextBox3.Text

'End Sub
 
G

Guest

First, you'll be spamming your users with Excel apps everytime they click
button1 :). Fix that first. Next change exsell.Workbooks.Add() to
exsell.Workbooks.Open("C:/temp/textexcell.xls") Without testing it, I'd say
that calling it again when it's open should just pop it to the front, but
check that to be sure.

Do you have a definate number of colums? because plotting by letter & number
is really only useful for very few cells, after that you might want to locate
cells by addressing Cells(row,column) This should act just like a range
object and allow you to select individual cells. You can either keep track
of the rows in particular to a single user, or search down for the next empty
row and keeping a RowIndex in the scope of your class. Everytime a row is
filled: RowIndex =+1 and call your columns.
cells(RowIndex,1),cells(RowIndex,2) for each colum you want. I'm guessing to
the number of text boxes you have.

Randy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top