Solved

VB6 - Active x - Excel Linked Cell Property

Posted on 2009-07-13
1
1,211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

623 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