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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using an Excel spreadsheet in VB6

Status
Not open for further replies.

peekay

Programmer
Oct 11, 1999
324
ZA
I have to select a cell in an Excel spreadsheet and put that value in a text box on my VB6 form. I can put the spreadsheet on my form by way of an OLE control but do not know how to program an event to transfer the value of a specific cell to a text box. Can somebody please help.
 
You can use MS automation as follows:<br>
<br>
Select the References option under Project and add<br>
the MS Excel 8.0 Object Library.<br>
Then try the following code in your app.<br>
<br>
'I have this within a command button.<br>
<br>
'----Begin<br>
Dim MyXL As New Excel.Application<br>
<br>
MyXL.Workbooks.Open (App.Path + &quot;\test.xls&quot;)<br>
' Where test.xls is your spreadsheet to get data from<br>
<br>
Worksheets(&quot;sheet1&quot;).Activate<br>
Worksheets(&quot;sheet1&quot;).Range(&quot;A1&quot;).Activate<br>
<br>
' Where sheet1 is the sheet containing your data and<br>
' Cell A1 is the cell you want data from.<br>
<br>
MsgBox ActiveCell.Value<br>
' Displays in a msgbox<br>
<br>
MyXL.Workbooks.Close<br>
<br>
Set MyXL = Nothing<br>
' --- End
 
Thanks Huntr1<br>
I will try this.<br>
Can you please also indicate how i can get the cell reference set as a variable which responds to a cell click event. In your code the cell reference is already selected in code - i wish that to be selected at runtime<br>
peekay
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top