LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing values to and from Excel

Solved!
Go to solution

All,

 

I am building a program to gather data from a force gauge via RS232. One of the requirements of the program is to have the user tell the program what sample they are on in case their samples are not in order. I also need to check my Excel sheet if that sample has already been tested. Some of my code in a module looks like this:

 

Function HasSampleBeenTested(dblSample As Double, strSG As String) As Boolean

 

' Finds out if the sample being tested has already been tested.
HasSampleBeenTested = False
Range("B14").Select

'First, find the Sample Group Number
Do Until ActiveCell.Value = ""
    If ActiveCell.Value = strSG Then
        ActiveCell.Offset(0, -1).Select
        Exit Do
    Else
        ActiveCell.Offset(1, 0).Select
    End If
Loop

 

' Next, find the actual sample
Do Until ActiveCell.Value = ""
    If ActiveCell.Value = strSample And ActiveCell.Offset(0, 1).Value = strSG Then
        HasSampleBeenTested = True
        Exit Do
    Else
        ActiveCell.Offset(1, 0).Select
    End If
Loop

End Function

 

I also have a diagram wired to this exact instance of Excel, but I'm not sure how to wire my type (boolean) and get the data out as a boolean via the Variant to Data VI.

0 Kudos
Message 1 of 9
(3,136 Views)
Solution
Accepted by BadAzzS10

If the datatype of the variant is a Boolean then all you need to do is to wire a Boolean constant (its value is irrelevant) to the Variant to Data to convert the variant to a Boolean.

 

Note: Are "Sample #" and "Previous Sample" supposed to be integer values? If so, you should make the datatype I32 instead of DBL. If the value is truly floating point, then you should NOT be using an equal function to compare floats. Please search on this topic (comparing floats).

 

Also, any particular reason why you're using a local variable for "Raw Data Report" instead of a wire?

0 Kudos
Message 2 of 9
(3,122 Views)

Yes, Sample Number and Previous Sample are supposed to be integer values.

 

Eariler on in my sequence, I create the file from a templete. I thought it would be easier doing a local variable. Is there a more acceptable solution?

0 Kudos
Message 3 of 9
(3,116 Views)

 


BadAzzS10 wrote: 

Eariler on in my sequence, I create the file from a templete. I thought it would be easier doing a local variable. Is there a more acceptable solution?


Yes. A wire. Local variables can "easily" create race conditions, so they're not as "easier" as most new LabVIEW programmers think. They have their uses, but for the most part they just lead to problems. See, for example, this thread of horror:

http://forums.ni.com/t5/BreakPoint/Why-some-people-say-Local-Variables-are-bad/td-p/711239

 

0 Kudos
Message 4 of 9
(3,110 Views)

Yikes...and I have them all over the place!

0 Kudos
Message 5 of 9
(3,097 Views)

 


@BadAzzS10 wrote:

Yikes...and I have them all over the place!


Somehow I'm not surprised... Smiley Wink

 

0 Kudos
Message 6 of 9
(3,092 Views)

One more thing, I'm getting lost on the Insert Into Array. I think it needs it to satisify the Excel Run Macro VI (that would be my Sample and the Sample Group that I send to the Excel module), but I'm getting a unwired or bad termal at that point.

0 Kudos
Message 7 of 9
(3,084 Views)

There is no reason to actually have an Insert Into Array. The Build Array already gives you an array. The Excel Run Macro actually wants an array of variants, not an array of strings. The red coercion dot indicates that your array of strings is being converted to an array of variants. I'm not sure if the macro will actually received the Sample # correctly since you're converting it to a string. The safe bet would be to convert each value to a variant (using the To Variant function) and build an array from that to feed to the Excel Run Macro:

Message 8 of 9
(3,075 Views)

That appears to have worked. Thank you for your time! I've learned quite a bit.

0 Kudos
Message 9 of 9
(3,067 Views)