By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,646 Members | 2,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,646 IT Pros & Developers. It's quick & easy.

RETRIEVING AND ADDING DATAS WITH EXCEL for VB6

P: 1
Good ones!! I want to retrieve and add datas with excel. I've tried one by adding two textboxes and two command buttons. THE code is:

'do declare these variables you need to add a reference'to the microsoft excel 'xx' object library.
'you need two text boxes and two command buttons'on the form, an excel file in c:\book1.xls
Dim xl As New Excel.Application
Dim xlsheet As Excel.Worksheet
Dim xlwbook As Excel.Workbook

Private Sub Command1_Click()
'the benifit of placing numbers in (row, col) is that you'can loop through different directions if required. I could'have used column names like "A1" 'etc.
Text1.Text = xlsheet.Cells(2, 1) ' row 2 col 1 Text2.Text = xlsheet.Cells(2, 2) ' row 2 col 2
'don't forget to do this or you'll not be able to open'book1.xls again, untill you restart you pc. xl.ActiveWorkbook.Close False, "c:\book1.xls"
xl.Quit
End Sub

Private Sub Command2_Click()
xlsheet.Cells(2, 1) = Text1.Text
xlsheet.Cells(2, 2) = Text2.Text
xlwbook.Save

'don't forget to do this or you'll not be able to open'book1.xls again, untill you restart you pc. xl.ActiveWorkbook.Close False, "c:\book1.xls"
xl.Quit
End Sub

Private Sub Form_Load()
Set xlwbook = xl.Workbooks.Open("C:\book1.xlsm")
Set xlsheet = xlwbook.Sheets.Item(1)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set xlwbook = Nothing
Set xl = Nothing
End Sub

BUT I CAN'T ADD THE DATAS MORE THAN ONE, THE NEW DATA ALWAYS REPLACE THE OLDER DATA.

And pliz suggest how to retrieve a data from that particular spreadsheet which the user prefer.
Nov 23 '07 #1
Share this Question
Share on Google+
1 Reply


kadghar
Expert 100+
P: 1,295
Good ones!! I want to retrieve and add datas with excel. I've tried one by adding two textboxes and two command buttons. THE code is:

...

BUT I CAN'T ADD THE DATAS MORE THAN ONE, THE NEW DATA ALWAYS REPLACE THE OLDER DATA.

And pliz suggest how to retrieve a data from that particular spreadsheet which the user prefer.
ok, i can see you've got 4 command buttons, one will load, other will unload a book, then one will put in the cells A2 and B2 the textbox values, and other will put in the text boxes that cell's values.

If you wanto to change the workbook, you can use a file dialog. or just create another textbox where the user can write the path. and replace the "C:\. ..." with the string or the textboxe's text
Nov 23 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.