473,588 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Excel: getting values out of a variant array, VBA into adjoining cells on worksheet

scubak1w1
53 New Member
Hello,


For a project I am working on, I wrote a piece of code in VBA backend of Excel to convert RGB colour values to HSV...

The VBA is working fine, and as you can see at the bottom I am stuffing the three results generated into an array expressed (sic) as a variant...

The first value in the returned array, Hue, is dropped into the cell where the function is called (E2 in the attached screen shot..) - i.e., "=fntHSVtoRGB(E2 ,F2,G2)"

In Excel do I get access to the 2nd and 3rd values in the array (i.e., saturation and value) and put in the correct adjacent cells? (i.e., cells F2 and G2, respectively, in the screen shot attached)

As you can see towards the bottom of the code, I have tested using a message box and so know that the array has them in there! Frustrating as you can imagine...

I am thinking there is some CTRL+SHIFT+ENTE R approach – but I don’t use Excel for arrays much, that is what databases are for! ;-)

I am guessing I could call three variations of the function, one for hue, saturation and value - but that does not seem very elegant!

[aside: Python has RGB --> HSV and back as a built in functions, guess it could be a good time to start using Python... <wink>]

Thanks in advance for any input!

Cheers:
GREG COCKS

----------------------------------------

Expand|Select|Wrap|Line Numbers
  1.  
  2. Function fntRGBtoHSV(intRed As Integer, intGreen As Integer, intBlue As Integer) As Variant
  3.  
  4.     'algorithm derived from http://www.easyrgb.com/index.php?X=MATH#text2
  5.  
  6.     Dim aryHSLResults As Variant
  7.  
  8.     Dim intMax As Integer
  9.     Dim intMin As Integer
  10.  
  11.     Dim dblWorkingR As Double
  12.     Dim dblWorkingG As Double
  13.     Dim dblWorkingB As Double
  14.  
  15.     Dim dblHue As Double
  16.     Dim dblSaturation As Double
  17.     Dim dblValue As Double
  18.  
  19.     Dim dblDelta As Integer
  20.  
  21.     intMax = fntMaximum(intRed, intGreen, intBlue)
  22.  
  23.     intMin = fntMinimum(intRed, intGreen, intBlue)
  24.  
  25.     dblDelta = intMax - intMin
  26.  
  27.     dblValue = intMax
  28.  
  29.     If (intMax = 0) Then
  30.  
  31.         'i.e., this is a gray, no chroma
  32.         dblHue = 0
  33.         dblSaturation = 0
  34.         dblValue = 0
  35.  
  36.     Else
  37.  
  38.         'i.e., a chromatic value
  39.         dblSaturation = dblDelta / intMax
  40.         dblWorkingR = (((intMax - intRed) / 6) + (dblDelta / 2)) / dblDelta
  41.         dblWorkingG = (((intMax - intGreen) / 6) + (dblDelta / 2)) / dblDelta
  42.         dblWorkingB = (((intMax - intBlue) / 6) + (dblDelta / 2)) / dblDelta
  43.  
  44.         If (intRed = intMax) Then
  45.             'red is max
  46.             dblHue = dblWorkingB - dblWorkingG
  47.         Else
  48.             If (intGreen = intMax) Then
  49.                 'green is max
  50.                 dblHue = (1 / 3) + dblWorkingR - dblWorkingB
  51.             Else
  52.                 If (intBlue = intMax) Then
  53.                     'blue is max
  54.                     dblHue = (2 / 3) + dblWorkingG - dblWorkingR
  55.                 End If
  56.             End If
  57.         End If
  58.  
  59.     End If
  60.  
  61.     If (dblHue < 0) Then
  62.         dblHue = dblHue + 1
  63.     Else
  64.         If (dblHue > 1) Then
  65.             dblHue = dblHue - 1
  66.         End If
  67.     End If
  68.  
  69.     'convert hue to degrees
  70.     dblHue = dblHue * 360
  71.  
  72.     'convert (sic) saturation to a percentage
  73.     dblSaturation = dblSaturation * 100
  74.  
  75.     'convert 'value' with a distinct multipler
  76.     dblValue = dblValue / cstValueMultipler
  77.  
  78.     'stuff the three results into an array
  79.     aryHSLResults = Array(dblHue, dblSaturation, dblValue)
  80.  
  81.     'DRAFT @ 12/15/10 - delete this message box call
  82.     MsgBox ("HSV: " & aryHSLResults(0) & ", " & aryHSLResults(1) & ", " & aryHSLResults(2))
  83.  
  84.     'send the values back
  85.     fntRGBtoHSV = aryHSLResults
  86.  
  87. End Function
  88.  
  89.  
Attached Images
File Type: jpg screen_shot.jpg (30.4 KB, 116 views)
Dec 16 '10 #1
0 2121

Sign in to post your reply or Sign up for a free account.

Similar topics

3
7538
by: Ali Tahbaz | last post by:
I'm having trouble iterating through LinkSources in an Excel workbook using C#. I first wrote the below code in VBA to get a quick, correct result, Dim x As Variant For Each x In ThisWorkbook.LinkSources Debug.Print x Next but am so far unable to successfully convert the code to workable
1
3819
by: MattB | last post by:
OK, never mind my last post. It was easy enough to refer to the table the repeater is bound to, but I made a big, incorrect assumption in that post. In my last post I thought I was successfully getting values from the textboxes in my repeater. It turns out I am not and I can't see what I'm doing wrong. I've had this problem before and just ended up taking a different route to avoid it. Now I REALLY need to figure out what I'm doing...
2
1687
by: Kiran Kumar Pinjala | last post by:
Hi, May be this is silly, or I just need a second pair eyes to look at this. I am trying to get values that I have edited in a datagrid and update the values with those values. Here is the code that I am using in my update command. TextBox txtProdName = (TextBox)e.Item.Cells.Controls; TextBox txtSuppID = (TextBox)e.Item.Cells.Controls; TextBox txtCatID = (TextBox)e.Item.Cells.Controls;
3
3870
by: Michael Glass | last post by:
I'm working on an ASP.Net web app using VS2005 and the .Net 2.0 framework, and I have a serious problem with the page I'm currently working on. The page has, among other things, two FormViews and a GridView control, each with its own SqlDataSource. FormView1 talks to my Opportunity table and has an ItemTemplate and an EditItemTemplate. FormView2 talks to my Activities table and has an ItemTemplate, InsertItemTemplate and an...
0
2074
by: sathya.krishnamurthy | last post by:
Hello Everybody I have an C++ COM DLL with the following implementation. __interface ICPM : IDispatch { HRESULT GlobalComputeClass ( VARIANT *inArr, double *pval); }
7
9530
by: Paul M. Cook | last post by:
Let's say you have a CSV file and you load it into a variant array using the split function on VBCrLF. Then you load a variable with the line count and loop through the array for 0 to line count. This works well unless you have blank lines at the end of the CSV file. Now if you process the loop, you'll get an out of bounds subscript at the end of the loop because you are referencing null values at the end of the variant array. How...
0
2019
by: dwinson | last post by:
I am writing an add-in in C# for a server written in VB6. In order for my add-in to work I need to implement this method: MyMethod(string inputData) .... so my C# code looks like this: public object MyMethod(string inputData)... I wrote a test harness in VB6 to test the method. I can call the method
4
5993
by: buzzluck68 | last post by:
Hello, I am having trouble upgrading my VB apps to .net. When i started at my company there was a dll created here in vb6, and i need to use that dll for many functions that would take too much time to re-create for the limit that i am given. I have to create a new app in .net, per my instructions, but the dll uses variant arrays, and i keep getting an error whenever i pass an object array from my .net app to the vb6 dll which needs...
2
2241
by: gellis72 | last post by:
I'm working on a program that imports a bunch of data from a folder full of Excel files and compiles it into an Access DB. The Excel files have a varying number of rows and columns that need to be imported, and the data needs to be processed/error checked before it's written to the DB. The way I've tackled this is to have a procedure that steps through a list of Excel files, invokes an instance of Excel, opens file "x", copies the relevant...
1
5626
by: Bonzs | last post by:
I have troule with this macro... geting the used rsnge... Public strName As String, ws As Worksheet Sub Test() Workbooks.Open Filename:= _ "C:\Documents and Settings\User\Desktop\IT Development\Pricer.xls" 'Begins formatting the pricer for generating the EPB... strName = ActiveSheet.Name 'MsgBox strName MsgBox "The Used Range of this Worksheet is: " & GetUsedRange(ws)
0
7857
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
7981
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8222
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6632
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5396
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3846
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2367
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1194
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.