Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1229
  • Last Modified:

VB6 - Active x - Excel Linked Cell Property

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
therealmongoose
Asked:
therealmongoose
1 Solution
 
therealmongooseAuthor Commented:

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now