|
-
Jan 17th, 2001, 09:27 AM
#1
Thread Starter
Member
Hi,
The following is the program i have written to input data into Excel using VB.It is working fine for inoutting one value for a particular cell.BUT i want to make it work like a database entry i.e i WANT TO USE A SINGLE TEXTBOX TO MAKE ENTRIES INTO SAY INCREASING ROW NUMBER(i.e row1,2,3..).I have tried using 'for' statement for incrementing row number but it doesn't seem to work.
Private Sub Command1_Click()
Dim XLApp As New Excel.Application
Dim i As Integer
XLApp.Workbooks.Open ("C:\My Documents\1.xls")
XLApp.Visible = True
Text1.SetFocus
For i=1 to 10
XLApp.Cells(i, 1) = Text1.Text
XLApp.ActiveWorkbook.Save
ReadOnlyRecommended = False
CreateBackup = False
Text1 = ""
Next i
XLApp.Workbooks.Close
Set XLApp = Nothing
End Sub
Please help!
-
Jan 17th, 2001, 10:14 AM
#2
You're putting too much in your for...neext statement:
Code:
Private Sub Command1_Click()
Dim XLApp As New Excel.Application, i As Integer
With XlApp
.Workbooks.Open ("C:\My Documents\1.xls")
.Visible = True
.displayalerts = false
End with
Text1.SetFocus
For i=1 to 10
XLApp.Cells(i, 1) = Text1.Text
Next i
Text1.text = ""
XLApp.ActiveWorkbook.Save
XLApp.Workbooks.Close
Set XLApp = Nothing
End Sub
Your code was working fine, you deleted the text in the text box though by selecting the text1.text = "". As the for...next part was excecuting, you deleted this text after the first cell, then the blank text was copied over to the remaining 9 cells 
I have also excluded the following lines, as these values are automatically set if you leave them out :
ReadOnlyRecommended = False
CreateBackup = False
-
Jan 17th, 2001, 10:28 AM
#3
Lively Member
Hi Hari,
They way I chuck stuff into Excel cells is to use the Range object.....
Code:
Dim liRow As Index
Dim lsCol As String
lsCol = "A"
For liRow = 1 To 10
loWorksheet.Range(lsCol & liRow).Value = "This is row No. " & liRow
Next
I cycle through (not literally as a road bike tends to find the excel surface too bumpy unless the gridlines are turned off!). As I wuz saying.... the cols are cycled through too, in some cases, using a quick algorithm to convert a number to column. With the Range object you can use the regular Excel notation to place the same data in multiple cells ("A1:C3" etc)
Anakim
It's a small world but I wouldn't like to paint it.
-
Jan 17th, 2001, 10:32 AM
#4
Lively Member
-
Jan 17th, 2001, 10:42 AM
#5
I've done that one too many times & have just about learnt my lesson from all the others on this site moaning
-
Jan 17th, 2001, 10:50 AM
#6
Thread Starter
Member
hi,
Thanks for the quick response.But WHATEVER I AM ENTERING IN TEXT! IS BEING REPEATED IN ALL THE ROWS.I don't want to do that i want to input new data to each row thru' the same textbox.I hope i make myself clear.
Waiting for ur response,
Hari
-
Jan 17th, 2001, 11:00 AM
#7
Lively Member
Of course
Of course it's being repeated in all rows as there's no mechanism for the user to change the contents of the text box.
Put the code in the lostfocus event on the text box (though I try to avoid using lostfocus/gotfocus events as a rule), or trap the 'return' key. When user hits return key move text to whichever row you want. Praps if there's a boat load of stuff the user is entering consider an array of text boxes or a grid and loop through those placing contents to cells as you go.
Depends on what you're trying to achieve.
Oh and
WHATEVER I AM ENTERING IN TEXT! IS BEING REPEATED IN ALL THE ROWS
please don't shout - beer is still wearing off!
Anakim
It's a small world but I wouldn't like to paint it.
-
Jan 17th, 2001, 11:01 AM
#8
Ok, something like:
Code:
Public i As Integer
Private Sub Form1_load
i = 1
End Sub
Private Sub Command1_Click()
Dim XLApp As New Excel.Application
With XlApp
.Workbooks.Open ("C:\My Documents\1.xls")
.Visible = True
.displayalerts = false
End with
Text1.SetFocus
XLApp.Cells(i, 1) = Text1.Text
i = i + 1
Text1.text = ""
XLApp.ActiveWorkbook.Save
XLApp.Workbooks.Close
Set XLApp = Nothing
End Sub
-
Jan 17th, 2001, 12:23 PM
#9
Thread Starter
Member
Yippee!!
it worked.Thanks a lot.
I will be coming up with such lousy mistakes if u don't mind!
Hari
-
Jan 18th, 2001, 12:07 AM
#10
Not a problem, I noticed I wrote that in a rush there, & that you're a new member.
If you just pasted the code ijnto your app & want me to explainwhat the hell it means, just ask...
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
|