Using math for negative minus negative - vb.net

this gives me 0 instead of -10. how can I fix it?
Dim aa As String = "-5"
Dim bb As String = "-5"
Debug.Print(Val(aa) - Val(bb))

Actually Its correct
Dim aa As String = "-5"
Dim bb As String = "-5"
Debug.Print(Val(aa) - Val(bb))
If you want -10 then you need to add
Dim aa As String = "-5"
Dim bb As String = "-5"
Debug.Print(Val(aa) + Val(bb))
because (-5) - (-5) becomes -5 + 5 so its 0

it is the right answer.
-5 - -5 = 0
if you want to get -10 just do
Dim aa = -5
Dim bb = -5
Debug.Print(aa + bb)

Related

Split and calculate string vb.net

I have kind of difficult question.
I have a a string like
2021-09-24 00:52:27
2021-09-23 03:46:08
2021-09-22 04:13:03
2021-09-21 04:08:02
2021-09-20 02:13:27
2021-09-19 05:27:38
2021-09-18 03:43:32
2021-09-17 03:23:44
I need to count the timespan of a last week.
The thing the string not always will be the last 7 days
so i made a string to get the last 7 days and compare them and if a line isnat matching the date it will be 00:00:00,
before that i splited the string.
before that i declared X as a number of lines in the string
2021-09-24
2021-09-23
2021-09-22
2021-09-21
2021-09-20
2021-09-19
2021-09-18
2021-09-17
00:52:27
03:46:08
04:13:03
04:08:02
02:13:27
05:27:38
03:43:32
03:23:44
Dim x As String = RichTextBox4.Lines.Count ' The number of lines with date and hours together
Dim a1 As String = Date.Now.ToString("yyyy-MM") ' Creating dates for the last 7 days
Dim a2 As String = Date.Now.ToString("dd")
Dim b1 As String = Date.Now.ToString("yyyy-MM")
Dim b2 As String = Date.Now.ToString("dd") - 1
Dim c1 As String = Date.Now.ToString("yyyy-MM")
Dim c2 As String = Date.Now.ToString("dd") - 2
Dim dd1 As String = Date.Now.ToString("yyyy-MM")
Dim dd2 As String = Date.Now.ToString("dd") - 3
Dim e1 As String = Date.Now.ToString("yyyy-MM")
Dim e2 As String = Date.Now.ToString("dd") - 4
Dim f1 As String = Date.Now.ToString("yyyy-MM")
Dim f2 As String = Date.Now.ToString("dd") - 5
Dim g1 As String = Date.Now.ToString("yyyy-MM")
Dim g2 As String = Date.Now.ToString("dd") - 6
Dim d1 As String = a1 + "-" + a2
Dim d2 As String = b1 + "-" + b2
Dim d3 As String = c1 + "-" + c2
Dim d4 As String = dd1 + "-" + dd2
Dim d5 As String = e1 + "-" + e2
Dim d6 As String = f1 + "-" + f2
Dim d7 As String = g1 + "-" + g2
Dim lines11 = RichTextBox4.Lines
Dim modifiedLines11 = New List(Of String)()
For index11 As Integer = 0 To lines11.Length - 1
Dim url11 = lines11(index11).Substring(lines11(index11).LastIndexOf(" "c))
modifiedLines11.Insert(index11, lines11(index11).Replace(url11, String.Empty))
modifiedLines11.Add(url11)
RichTextBox4.Text = String.Join(Environment.NewLine, modifiedLines11)
Next
' after split to 2 lists in string
Dim o1 As String = RichTextBox4.Lines(0) ' The first lines of the string is the dates
Dim o2 As String = RichTextBox4.Lines(1)
Dim o3 As String = RichTextBox4.Lines(2)
Dim o4 As String = RichTextBox4.Lines(3)
Dim o5 As String = RichTextBox4.Lines(4)
Dim o6 As String = RichTextBox4.Lines(5)
Dim o7 As String = RichTextBox4.Lines(6)
Dim l1 As String ' This is the second list in the string those are the times
Dim l2 As String
Dim l3 As String
Dim l4 As String
Dim l5 As String
Dim l6 As String
Dim l7 As String
here im trying to compare it to the actual date and if isnt it will give me 00:00:00.
If o1 = d1 Then
l1 = RichTextBox4.Lines(x)
Else
l1 = "00:00:00"
If o2 = d2 Then
l2 = RichTextBox4.Lines(x + 1)
Else
l2 = "00:00:00"
If o3 = d3 Then
l3 = RichTextBox4.Lines(x + 2)
Else
l3 = "00:00:00"
If o4 = d4 Then
l4 = RichTextBox4.Lines(x + 3)
Else
l4 = "00:00:00"
If o5 = o5 Then
l5 = RichTextBox4.Lines(x + 4)
Else
l5 = "00:00:00"
If o6 = d6 Then
l6 = RichTextBox4.Lines(x + 5)
Else
l6 = "00:00:00"
If o7 = d7 Then
l7 = RichTextBox4.Lines(x + 6)
Else
l7 = "00:00:00"
End If
End If
End If
End If
End If
End If
End If
' here im trying to calculate the time.
Dim ts1 As TimeSpan = TimeSpan.Parse(l1)
Dim ts2 As TimeSpan = TimeSpan.Parse(l2)
Dim ts3 As TimeSpan = TimeSpan.Parse(l3)
Dim ts4 As TimeSpan = TimeSpan.Parse(l4)
Dim ts5 As TimeSpan = TimeSpan.Parse(l5)
Dim ts6 As TimeSpan = TimeSpan.Parse(l6)
Dim ts7 As TimeSpan = TimeSpan.Parse(l7)
Dim ttsum As TimeSpan = ts1 + ts2 + ts3 + ts4 + ts5 + ts6 + ts7
MsgBox(ttsum.ToString)
In my opinion the porblem with the code is or the method is bad or the if statements arent good with this method.
Since I don't see any AM/PM data, I am assuming the data in the rich text box is in 24 hour format.
Your code is so complicated because you are manipulate strings instead of dealing with dates. If you want to compare dates and manipulate dates, the first thing you need to do is turn your strings into dates. Since we do not know how many items we are dealing with we will use List(Of T).
Private Function GetListOfDates() As List(Of Date)
Dim lst As New List(Of Date)
Dim provider As CultureInfo = CultureInfo.InvariantCulture
For Each line In RichTextBox1.Lines
' 2021-09-17 03:23:44
lst.Add(Date.ParseExact(line, "yyyy-MM-dd hh:mm:ss", provider))
Next
Return lst
End Function
Next you need to filter the list for last week. For your purposes, a day starts as 00:00:00 and ends at 23:59:59
Private Function FilterListForLastWeek(lst As List(Of Date)) As List(Of Date)
Dim DatesInWeek As New List(Of Date)
'I hardcoded the following date to work with your sample data
Dim LastDayOfWeek = New Date(2021, 9, 24, 23, 59, 59)
'You would use the following code in the real version
'Dim LastDayOfWeek = New Date(Date.Now.Year, Date.Now.Month, Date.Now.Day, 23, 59, 59)
Debug.Print(LastDayOfWeek.ToString)
Dim FirstDayOfWeek = LastDayOfWeek.AddDays(-6)
FirstDayOfWeek = New Date(FirstDayOfWeek.Year, FirstDayOfWeek.Month, FirstDayOfWeek.Day, 0, 0, 0)
Debug.Print(FirstDayOfWeek.ToString)
For Each d In lst
If d >= FirstDayOfWeek AndAlso d <= LastDayOfWeek Then
DatesInWeek.Add(d)
End If
Next
Return DatesInWeek
End Function
It looks like you are considering the hour, minute, second portion of the date as a time span. To add TimeSpan you use the Add method. The last date in the data from the rich text box (the 17th) is excluded since it is not in the week (days 18-24 inclusive is 7 days). The message box displays 1:00:24:17 which is 1 day, zero hours, 24 minutes, and 17 seconds. You can also display as TotalHours, TotalMinutes, etc..
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim AllDates = GetListOfDates()
Dim DatesInWeek = FilterListForLastWeek(AllDates)
Dim TimeSpanSum As TimeSpan
For Each d In DatesInWeek
TimeSpanSum = TimeSpanSum.Add(New TimeSpan(d.Hour, d.Minute, d.Second))
Next
MessageBox.Show(TimeSpanSum.ToString)
End Sub

in vb.net I want to loop charts

I want listbox1 item =
aa
ab
ac
ba
bb
bc
ca
cb
cc
And this my code
Dim text As String = "abc"
Dim i As Integer = 0
Do
ListBox1.Items.Add(text.Chars(i))
i += 1
Loop Until (i = text.Length)
Use this, you need 2 loops:
For i as Integer = 0 to 2
For j as Integer = 0 to 2
ListBox1.Items.Add(text.Chars(i) & text.Chars(j))
Next
Next

VBA Error: "The object invoked has disconnected from its clients"

I am attempting to write a macro that matches up x/y coordinates to ellipses that they fit into. I get the automation error at the "Else" line in my code. I have reviewed a lot of other posts but I can't figure out what is wrong with my code. Any assistance is much appreciated. Thank you!
Private Sub CommandButton1_Click()
Dim XR As Integer
Dim XC As Integer
Dim YR As Integer
Dim YC As Integer
Dim areaR As Integer
Dim areaC As Integer
Dim hR As Integer
Dim hC As Integer
Dim kR As Integer
Dim kC As Integer
Dim aR As Integer
Dim aC As Integer
Dim bR As Integer
Dim bC As Integer
Dim angleR As Integer
Dim angleC As Integer
Dim matchR As Integer
Dim matchC As Integer
XR = 2
XC = 1
YR = 2
YC = 2
Do Until ThisWorkbook.Sheets("Sheet1").Cells(XR, XC).Value = ""
ThisWorkbook.Sheets("Sheet1").Activate
areaR = 2
areaC = 6
hR = 2
hC = 7
kR = 2
kC = 8
aR = 2
aC = 9
bR = 2
bC = 10
angleR = 2
angleC = 11
matchR = XR
matchC = 12
Do Until ThisWorkbook.Sheets("Sheet1").Cells(hR, hC).Value = ""
If (((((ThisWorkbook.Sheets("Sheet1").Cells(XR, XC).Value) _
- (ThisWorkbook.Sheets("Sheet1").Cells(hR, hC).Value)) * _
Cos((ThisWorkbook.Sheets("Sheet1").Cells(angleR, angleC).Value)) _
+ ((ThisWorkbook.Sheets("Sheet1").Cells(YR, YC).Value) - _
(ThisWorkbook.Sheets("Sheet1").Cells(kR, kC).Value)) * Sin _
((ThisWorkbook.Sheets("Sheet1").Cells(angleR, angleC).Value))) ^ 2) _
/ ((Cells(aR, aC).Value) ^ 2)) + (((((Cells(XR, XC).Value) - _
(Cells(hR, hC).Value)) * Sin((ThisWorkbook.Sheets("Sheet1").Cells _
(angleR, angleC).Value)) - ((ThisWorkbook.Sheets("Sheet1").Cells(YR, YC).Value) _
- (ThisWorkbook.Sheets("Sheet1").Cells(kR, kC).Value)) _
* Cos((Cells(angleR, angleC).Value))) ^ 2) / ((Cells(bR, bC).Value) ^ 2)) _
<= 1 Then
ThisWorkbook.Sheets("Sheet1").Cells(matchR, matchC).Value = _
ThisWorkbook.Sheets("Sheet1").Cells(areaR, areaC)
Else
areaR = areaR + 1
hR = hR + 1
kR = kR + 1
aR = aR + 1
bR = bR + 1
angleR = angleR + 1
End If
Loop
XR = XR + 1
YR = YR + 1
Loop
End Sub
That's a lot of code, and doing direct math with cell reference values is very hard to read...probably even for you.
It may not be much of an answer, but if you create variables (yes, more variables), inside your loops, then your code will be easier to read for everyone and very possibly your error will emerge.
So instead of using ThisWorkbook.Sheets("Sheet1").Cells(XR, XC).Value in your calculation, first do this: X = ThisWorkbook.Sheets("Sheet1").Cells(XR, XC).Value
But don't stop there. Include intermediate variable that will self-describe your code and the calculation process.
This will hopefully allow you to see the code for what it really does.
isMatch = cosX <= 1
If isMatch Then
And, by the way, my suspission is that your error is coming from the fact that you are not qualifying all of your Cells methods with a sheets reference.

WorksheetFunction.Match not working

Here is an excerpt from my data:
------+------+------+------+------+
| A | B | C | D |
------+------+------+------+------+
1 | 10 20 25 30
2 | 152 181 195 210
and my code:
Dim xrng as range, yrng as range, offset as integer
set xrng = Sheets("Sheet1").Range("A1:D1")
set yrng = Sheets("Sheet1").Range("A2:D2")
offset = WorksheetFunction.Match(23, xrng , 1) - 1
Why does running this result in a 1004 error: Unable to get the match property of the worksheetfunction class? How can I fix it?
EDIT: Detailed problem
Okay, I have written a function that does interpolation:
Public Function interpolate(intvalue_X As Double, xrange As range, yrange As range) As Double
....this is just an excerpt:
Dim offst As Integer
offst = WorksheetFunction.Match(intvalue_X, xrange, 1) - 1 'find the offset of the nearest value
---
End Function
With the following data and call, it works fine and returns the correct answer:
(don't mind the variables' who's declarations aren't shown - they have been declared at this point, it's just not copied)
Set intXrng = Sheets("Tables").range("B32:G32")
If beltWidth >= 46 And beltWidth <= 122 And conveyerCenter >= 7.6 And conveyerCenter <= 152.4 Then 'dan kan jy die tabel gebruik
m = interpolate(beltWidth, intXrng, Sheets("Tables").range("B44:G44"))
c = interpolate(beltWidth, intXrng, Sheets("Tables").range("B45:G45"))
powerX = m * conveyerCenter + c
Else
MsgBox "Unable to use the power x-factor table.", vbCritical
End If
Now, when I use the same function, but with this data and call, it gives the error:
Set intXrng = Sheets("Tables").range("F4:I4")
angleSurcharge = 23
capacityTable = interpolate(angleSurcharge, intXrng, Sheets("Tables").range("F7:I7"))
Your values are not stored as strings because they are in a table header. Table headers are always read as strings regardless of their format.
You can convert all values to the Doubles before passing it to Worksheet.Match to fix the bug.
Dim offst As Integer
Dim arry As Variant
ReDim arry(1 To 1, 1 To xrange.Columns.Count)
For i = 1 To xrange.Columns.Count
arry(1, i) = CDbl(xrange.Cells(1, i).Value)
Next
offst = WorksheetFunction.Match(intvalue_X, arry, 1) - 1 'find the offset of the nearest value

how do i use 4 digit numbers in vb

I need to count from 0 - 9999 in VB. How can I make the format 0000 - 9999, so that the output is:
0000, 0001, 0002, 0003, .......
I used the following code
Dim p4num As Integer = 0
Dim p3num As Integer = 0
Dim p2num As Integer = 0
Dim p1num As Integer = 0
p4num += 1
If p4num = 10 Then
p4num = 0
p3num += 1
If p3num = 10 Then
p3num = 0
p2num += 1
If p2num = 10 Then
p2num = 0
p1num += 1
End If
End If
End If
but I would do it in another way. Any idea?
The ToString() may help. How about
myint.ToString("0000")
or
myint.ToString("D4");
For i as integer = 0 to 9999
Debug.WriteLine(i.ToString("0000"))
next
Untested code, but should work...
Your guide
Dim numbers As IEnumerable(Of Integer) = Enumerable.Range(0, 9999).ToArray()
'object j = string.Join(", ", numbers);
For Each item As var In numbers
Console.WriteLine(item.ToString("#0000"))
Next