If/Else statement for chart - vb.net

So, I've been stuck in this problem for so many days now. I'm trying to loop the system.array from the data in Excel. I use the if statement that read if the data value from the excel is numeric or not. After that I put elseif statement that change the chart marker style to none if the value is equal to zero. However, no matter what kind of methods that I try, the markers still show on the chart. Here's my code:
Dim A_GTRng As Excel.Range
Dim A_GTArry As System.Array
'Set the range
A_GTRng = excelWS.Range("I2", excelWS.Range("I2").End(Excel.XlDirection.xlDown))
'Read in the values of a range of cells
A_GTArry = CType(A_GTRng.Value, System.Array)
'Looping through the A_GTArry
For x As Integer = 1 To A_GTArry.GetUpperBound(0)
For y As Integer = 1 To A_GTArry.GetUpperBound(1)
Dim A_GT As Object = A_GTArry(x, y)
If IsNumeric(y).Equals(0) Then
'Chart1.Series("A_GT").MarkerSize = 0
'Chart1.Series("A_GT").MarkerColor = Color.Transparent
Chart1.Series("A_GT").MarkerStyle = MarkerStyle.None
ElseIf IsNumeric(y) Then
'Chart1.Series("A_GT").Points.Dispose()
Chart1.Series("A_GT").Points.AddY(A_GT)
Chart1.Series("A_GT").MarkerStyle = MarkerStyle.Diamond
Chart1.Series("A_GT").MarkerColor = Color.Red
Chart1.Series("A_GT").MarkerSize = 7
End If
Next y
Next x
Result from the program above:
Can anyone check if I make some mistake somewhere? I try to solve this problem since the past few days, but did got the result that I want. My brain pretty much fried now. I really need help to solve this. Thank you!

Since y is an Integer by definition in your For loop it will always be numeric.
Therefore this statement will never succeed:
If IsNumeric(y).Equals(0) Then
What exactly are you trying to do? Seems like you've gotten a little bit off course.
Z

Related

How to sum variables in VBA to check input data is correct?

May seem like a stupid question, but how do you sum variables in VBA to check input data is correct?
I'm trying to check the user has inputted data correctly into a UserForm before they continue to the next page. To do this I want to sum some of their input variables. If they don't input it correctly, they get a message box telling them to revise the numbers. My code is:
Dim BinQnt As Double
Dim FillQnt As Double
Dim FineQnt As Double
Dim CoarQnt As Double
Dim RAPQnt As Double
Dim CRQnt As Double
If BinQnt + FillQnt + FineQnt + CoarQnt + RAPQnt + CRQnt = 100 Then
'Code here for inserting values into database. Omitted to save space and confusion.
Else
MsgBox "Error, please check mixture design sums to 100%."
End If
When I'm testing it, it always goes to the error message box and I'm not sure why. Very sure I am adding variables which sum to 100 (haha). I first tried it without defining the variables, and now I have it still doesn't work.
Am I missing something obvious?
Do you want to round it or not?
In your question, you have the variables as Doubles, and in the answer as integers. Having them as integers will make it easier to hit 100%, but it will round to the nearest integer (2,4 = 2 | 2,6 = 3)
I will assume you are using all texboxes in the form, and as such use this code that is universal:
Dim tot As Double
tot = 0
For Each c In Me.Controls
If TypeName(c) = "TextBox" And IsNumeric(c.Value) Then
tot = tot + c.Value
End If
Next c
If tot = 100 Then
'Code here for inserting values into database. Omitted to save space and confusion.
Else
MsgBox "Error, please check mixture design sums to 100%. Currently at " & tot & "%."
End If
This would give us something like this:
However, using Integer or Long, gives us this:
Also, the reason as to why I'm using IsNumeric(c.Value), is to stop the code from failing in case of empty boxes (or filled with invalid entries).
You could also place a check here in case no boxes are allowed to be empty.

Peak over treshold method - VBA - Excel

I really hope you can help me with the following question.
I have a dataset and I need to filter for some values. This is a dataset for wave analysis, and I need to filter out for the higstest values. The so called peak over treshold method. I want to filter out the storms in de wave dataset.
I can do this with the following: A storm is defined when the waveheight is above a certain value, for example 2.5 meters. The waveheight is measured every 3 hours.
So in the dataset I can do an If(B3>$A$1; B3;0). A1 = 2,5 m. Now I have filtered for waveheights. So now I can see in my dataset rows straight after each other with values higher than 2,5 meters, because a storm last serveral hours. See picture 1 as example:
[Picture 1][1]
So if a certain value is above X, then return that value, otherwise return zero. Now comes the hard part:
Now I need the highest value of that strip for values above 2,5 meters. As this is a big(!) dataset, I have a lot of datapoints. So I have several 'storms', which don't always last as long. So the values that are higher than 2,5 meters aren't always 3 rows, sometimes more. See picture 2picture 2
I need to compute the highst value in Column i, for each storm. Because each storm doens't last as long, I probally need to write code in VBA. Could anybody help me with this? I've been stuck for 6 days, but my VBA code doens't work and I do not have much experience with VBA. If anybody could help me that would be very much appreciated!
Kind regards,
Jeroen
EDIT as a reply on -Excel Developers-
I think I'm almost there, thank u very much for replying!! I am probably misreading the code, but a I have still a slight difficulty in computing the highstest value for each storm I think it should look something like this, as seen in picture 3.Picture 3
The highest value of each storm should be printed in the next column, as seen in picture 3. It probably says it all in the code, but I am misreading it.
Thank u very much for you answer! I really hope you can help me with the last part :-)
Kind regards,
Jeroen
You will have to iterate over all the values in column i. For every storm, find the highest value. A storm starts when a 0 is followed by a positive value, and ends when a positive value is followed by a 0. So something like this:
Sub FindHighestValue()
Dim vData As Variant
Dim Storms As Collection
Dim ndx As Long
Dim ndxStorm As Long
Dim MaxValue As Double
Dim Previous As Double
Dim InStorm As Boolean
vData = Sheet1.Range("I8:I21").Value 'Change this to whatever range your wave heights are in
Set Storms = New Collection
Previous = 0
InStorm = False
For ndx = LBound(vData, 1) To UBound(vData, 1)
If vData(ndx, 1) > 0 And Previous = 0 Then
'A storm has started
MaxValue = vData(ndx, 1)
InStorm = True
End If
If InStorm And vData(ndx, 1) > MaxValue Then
MaxValue = vData(ndx, 1)
End If
If (vData(ndx, 1) = 0 And Previous > 0) Or (ndx = UBound(vData, 1)) Then
'A storm has ended
InStorm = False
Storms.Add MaxValue
End If
Previous = vData(ndx, 1)
Next
If Storms.Count > 0 Then
For ndx = 1 To Storms.Count
Debug.Print ndx & ": " & Storms(ndx)
Next
End If
End Sub

LOOP and nested if statements not working

the code is intended to seek out the "Yes" in "OverExtensions" and will create a new range and determine the maximum value of the new range. For some reason the logic of the code seems alright but I'm getting an empty result and would love the community's input as to where I've went wrong.
Private Sub CommandButton1_Click()
Dim maximum As Double, OverExtension As Range, x As Range
Cells.Interior.ColorIndex = 0
Set OverExtension = Range("D2:D12")
maximum = WorksheetFunction.Max(Range("c2:c12"))
For Each x In OverExtension
If x.Value = Yes Then
If x.Offset(0, -1).Value = maximum Then
x.Cells.Interior.ColorIndex = 22
End If
End If
Next x
End Sub
You nested the if and for blocks in a wrong way.
You have to put the second End If between Next PriceNo and Next OEResult.
Update:
You need just one End If. The first one is wrong because If and Then are in one line.
Besides you have problem with your OEYes and OEResult variables as they are defined but not initialized but as I don't know your intention I also don't know how to solve it.

Sum of elements (4) on mulitple lines in 2d array (txt file)

I have a text file that reads:
Left Behind,Lahaye,F,7,11.25
A Tale of Two Cities,Dickens,F,100,8.24
Hang a Thousand Trees with Ribbons,Rinaldi,F,30,16.79
Saffy's Angel,McKay,F,20,8.22
Each Little Bird that Sings,Wiles,F,10,7.70
Abiding in Christ,Murray,N,3,12.20
Bible Prophecy,Lahaye and Hindson,N,5,14.95
Captivating,Eldredge,N,12,16
Growing Deep in the Christian Life,Swindoll,N,11,19.95
Prayers that Heal the Heart,Virkler,N,4,12.00
Grow in Grace,Ferguson,N,3,11.95
The Good and Beautiful God,Smith,N,7,11.75
Victory Over the Darkness,Anderson,N,12,16
The last element of each line is a price. I would like to add up all the prices. I've been searching for so many hours now and cannot find a thing to answer my question. This seems soooo easy but I cannot figure it out!!! Please help out. BTW, this list is bound to change (adding of lines, deletion of lines, altering of lines) so if you can, please nothing concrete but instead leave the code open to changes. Thanks!!!
Just so you can see my pooooorrrr work, here is what I have (I think I deleted my code and rewrote a different way for several hours now.):
Dim Inv() As String = IO.File.ReadAllLines("Books.txt")
Dim t As Integer = Inv.Count - 1
Dim a As Integer = 0 to t
Dim sumtotal As String = sumtotal + Inv(4)
also,
for each line has either an "F" or an "N". how do I add up all the F's and all the N's. Do I do it via if statements?
First, you'll be better off using Double as your type instead of String. Second, observe how I use the Split function on each line, cast its last element as a double, and add it to the total. Yes, using an If Statement is how you can determine whether or not to add to the count of F or the count of N.
Dim lstAllLines As List(Of String) = IO.File.ReadAllLines("Books.txt").ToList()
Dim dblTotal As Double = 0.0
Dim intCountOfF As Integer = 0
Dim intCountOfN As Integer = 0
For Each strLine As String In lstAllLines
Dim lstCells As List(Of String) = strLine.Split(",").ToList()
dblTotal += CDbl(lstCells(3))
If lstCells(2) = "F" Then
intCountOfF += 1
Else
intCountOfN += 1
End If
Next

Inspecting a Word mail merge data source programmatically

I want to iterate over all rows of a MS-Word mail merge data source and extract the relevant data into an XML.
I'm currently using this code:
Imports Microsoft.Office.Interop
Do
objXW.WriteStartElement("Recipient")
Dim objDataFields As Word.MailMergeDataFields = DataSource.DataFields
For Each FieldIndex As Integer In mdictMergeFields.Keys
strValue = objDataFields.Item(FieldIndex).Value
If Not String.IsNullOrEmpty(strValue) Then
strName = mdictMergeFields(FieldIndex)
objXW.WriteElementString(strName, strValue)
End If
Next
objXW.WriteEndElement()
If DataSource.ActiveRecord = LastRecord Then
Exit Do
Else
DataSource.ActiveRecord = Word.WdMailMergeActiveRecord.wdNextDataSourceRecord
End If
Loop
And it turns out to be a little sluggish (About 1 second for each row). Is there any way to do it faster?
My fantasy is finding a function like MailMergeDataSource.ToDatatable and then inspecting the datatable.
Any time you're iterating through something row by row, and then doing some kind of processing on each row, is going to get a little slow.
I would be inclined to approach this problem by having a step before this which prepared the mdictMergeFields collection so that it only contained elements that were not 'null or empty', this will mean you won't have to check for that on each iteration. You could do this in process, or 'sneakily' in the background while the user is doing something else.
The other thing to try (might help!) is to change the "Do... Loop" block so that you're not checking at the end of each imported row whether or the record is the 'last record'. Instead, get a count of the records, and then compare the current index to the knowm maximum (which might be quicker)
I.E.:
Dim i, x as Integer
i = ActiveDocument.MailMerge.DataSource.RecordCount
Do While x < i
objXW.WriteStartElement("Recipient")
Dim objDataFields As Word.MailMergeDataFields = DataSource.DataFields
For Each FieldIndex As Integer In mdictMergeFields.Keys
strValue = objDataFields.Item(FieldIndex).Value
If Not String.IsNullOrEmpty(strValue) Then
strName = mdictMergeFields(FieldIndex)
objXW.WriteElementString(strName, strValue)
End If
Next
objXW.WriteEndElement()
x += 1
Loop
I don't really work with the Office Interop much, but hopefully this might offer some assistance! Post back, let me know how it goes.
/Richard.