Solved

VB6 - Active x - Excel Linked Cell Property

Posted on 2009-07-13
1
1,187 Views
Last Modified: 2013-12-20
I'd like to create an ocx control using vb6 for use in Excel (2003 version). I need the control to be linked to a cell in excel and display the value that is in the linked cell in a text box on the control. I've got as far as creating a property using get/set statement in the control, but can't seem to link it in to a cell in excel.... Any help much appreciated...
0
Comment
Question by:therealmongoose
1 Comment
 
LVL 10

Accepted Solution

by:
therealmongoose earned 0 total points
ID: 24852636

Thanks to anyone who took the time to look at my question - even if you thought that's a bit too much to explain... For your information, this is a first step towards and excel speedometer dial control (I want a neater solution than a pie chart graph) and need to read a cell value from the parent worksheet.

As a bit of a noob when it comes to building ocx controls in vb6, it's taken a fair amount of uphill learning to find the solution for this, so for the sake of someone elses sanity here's the answer...

The steps below are shown in a "for dummys" format, mainly on the grounds that this is what I would have liked to have found somewhere... :-)

To create an ocx object that reads from a linked cell in Excel...
Open vb6
Select Activex Control

First Create a User property:
---------------------------------------
 
From the projects menu, choose Add user control
Choose VB Control Interface Wizard and press open
Click next >
Remove all other properties (you can add them back in later if you need them) by clicking the double left button
Click next
Click New
Enter a name for your property (e.g. CellValue)
Click ok
Click Next > Next > Next > Finish

Then Link the property to a data binding:
--------------------------------------------------------
From the Tools menu choose Procedure Attributes
In the drop down find your property then click advanced
Select "User Interface default"
Select "Property is data bound"
Select "This Property binds to Datafield"
Select "Update Immediately"
Click OK

To test the control, stick a command button on the form in the on click event with the code:
MsgBox Me.CellValue
Build the ocx using File menu > Make Project1.ocx (save the file where you can find it!!)

Register the ocx on your PC
--------------------------------------
Open the cmd window (Start>run>cmd)
and register the ocx by typing
(assumes the ocx file is in c:\ root directory)
cd c:\
regsvr32 c:\project1.ocx
(to unregister the ocx use the U switch e.g. regsvr32 /u c:\project1.ocx

Test the Property:
------------------------
Open excel and insert a copy of your ocx - to do this you'll need to open the control toolbox (right click menu bar) and find the hammer and
spanner button - scroll down and find your control under Project1.UserControl1
Select the control and drop it in the worksheet
Right click the control and choose properties
If all has gone to plan there will be a LinkedCell property shown - Enter a cell reference (e.g. A1) then close the property window and click the design view button (blue triangle) on the control toolbox menu to turn off design mode.
Enter a value in cell A1
Click the command button on the ocx a couple of times (first click gets focus but does not seem to activate the on click event) The value of A1 should be shown in a message box...
Here's a couple of sites that have been useful with the above:
http://visualbasic.freetutes.com/learn-vb6-advanced/lesson13/p30.html http://pntpm3.ulb.ac.be/Info/Activex/ http://www.codeproject.com/KB/COM/ExcelActiveXArrow.aspx
Leave me a comment if this has helped you!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I made this because I wanted to get e-mail with a attached csv file so I'd would be able to import user input into a MS Excel template, but I also wanted to register/save all inputs from each day in a file on the server. 1st - It creates a temp C…
In this article you will learn how to create a free basic website on Bitbucket, a git service provider. Polymer creates dynamic HTML components, which allow more flexibility than static HTML. This tutorial uses Ubuntu Linux but can also be done on W…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now