Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting data from VB to Excel

Status
Not open for further replies.

singoi

Technical User
Mar 1, 2005
46
DE
hallo,

please anybody give me a small example programm where in i insert values from VB into excel sheet.

i give values in VB example 3 textboxes and click ok

this ok should execute Excel sheet and overwrite these values in particulat cells

example.

cell D1 = Text1.text
cell H1 = Text2.text
cell R1 = Text3.text

how can we define this
Thanks in advance for ur help.

singoi
 
Code:
    Set objExcel = CreateObject("Excel.Application")
    objExcel.DisplayAlerts = False
    objExcel.Workbooks.Open "c:\test\ExcelFile.xls"
    Set objBook = objExcel.ActiveWorkbook 
    Set objSheet = objBook.ActiveWorksheet' or objBook.Worksheets(3) or objBook.Worksheets("sd")
    With objSheet
         .Cells(2, 2).Value = Text1
         .Cells(3, 2).Value = Text2
         .Cells(4, 2).Value = Text3
    End With
    objBook.Save
    Set objSheet = Nothing
    Set objBook = Nothing
    objExcel.Quit
    Set objExcel = Nothing
 
Thanks Jerry,

where should we write this code.

should it be written in Module under Public variables or in Normal code under a button press event.

how should we define those objexcel, objBook and objSheet.

as a Public variables?

i want to write a code in Butten Press event so that the values been overwritten in particular TEST.xls file.

Thanks Jerry-

Singoi
 
Those variables sould be declared exactly where you need them.

Dim objExcel As Object
Dim objBook As Object
Dim objSheet As Object

Place the cursor inside Dim and press F1. Then read also about Public.

-----------------------
I hope this is not an excercise for your VB course!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top