I have 2 strings of numbers: like this:
Textbox1.Text = 1,2,3,4,5,6,7,8,9,11
Textbox2.Text = 11,9,8,7,6,5,4,3,2,1
I want to calculate, display in textbox3, their sum, + or - or * or /
all value from textbox1.text + all values from textbox2.text
Textbox3.Text = 12,11,11,11,11,11,11,11,11,12
I would like, though, and a specification where I can change the sign, instead of + being -, or / or * depending on my choice.
this code is a bit cumbersome, I would like to improve it with something simpler.
Dim str1 As String
'Set your string value
str1 = TxtBoxLstDrawsPlus.Text
Dim str2 As String
'Set your string value
str2 = TxtBoxLstDrawsMinus.Text
Dim strWords As String() = str1.Split(",")
Dim strWordsAAA As String() = str2.Split(",")
TextBox6.Text &= Val(strWords(0)) + Val(strWordsAAA(0)) & "," & Val(strWords(1)) + Val(strWordsAAA(1)) & "," & Val(strWords(2)) + Val(strWordsAAA(2)) & "," & Val(strWords(3)) + Val(strWordsAAA(3)) & "," & Val(strWords(4)) + Val(strWordsAAA(4)) & "," & Val(strWords(5)) + Val(strWordsAAA(5)) & "," & Val(strWords(6)) + Val(strWordsAAA(6)) & "," & Val(strWords(7)) + Val(strWordsAAA(7)) & "," & Val(strWords(8)) + Val(strWordsAAA(8)) & "," & Val(strWords(9)) + Val(strWordsAAA(9)) & "," & Val(strWords(10)) + Val(strWordsAAA(10)) & "," & Val(strWords(11)) + Val(strWordsAAA(11)) & "," & Val(strWords(12)) + Val(strWordsAAA(12)) & "," & Val(strWords(13)) + Val(strWordsAAA(13)) & "," & Val(strWords(14)) + Val(strWordsAAA(14)) & "," & Val(strWords(15)) + Val(strWordsAAA(15)) & "," & Val(strWords(16)) + Val(strWordsAAA(16)) & "," & Val(strWords(17)) + Val(strWordsAAA(17)) & "," & Val(strWords(18)) + Val(strWordsAAA(18)) & "," & Val(strWords(19)) + Val(strWordsAAA(19))
You can split the input strings (TextBox1.Text and TextBox2.Text), Zip the string arrays to produce a result array, and finally String.Join it in TextBox3:
Here's in one line:
TextBox3.Text = String.Join(",", TextBox1.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries).
Zip(TextBox2.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries),
Function(x, y) CInt(x) + CInt(y)))
You can use the same approach to do different arithmetic operations:
Dim opr As Char = "+"c '<- From your arithmetic operation selector like ComboBox.
TextBox3.Text = String.Join(",", TextBox1.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries).
Select(Function(x) CInt(x)).
Zip(TextBox2.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries).
Select(Function(y) CInt(y)),
Function(x, y)
Select Case opr
Case "-"c
Return x - y
Case "*"c
Return x * y
Case "/"c
Return x \ y
Case Else
Return x + y
End Select
End Function))
It'll be a good idea if you'd validate your inputs first:
Dim Arr1() = TextBox1.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
Dim Arr2() = TextBox2.Text.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
Dim opr As Char = OprTextBox.Text.ElementAtOrDefault(0) 'A TextBox this time as another example.
Dim ValidOpr() As Char = {"+"c, "-"c, "*"c, "/"c}
If Arr1.Length <> Arr2.Length OrElse
Arr1.Any(Function(x) Not Integer.TryParse(x, Nothing)) OrElse
Arr2.Any(Function(x) Not Integer.TryParse(x, Nothing)) OrElse
Not ValidOpr.Contains(Opr) Then
MessageBox.Show("Excuse me ... !?!?")
Return
End If
'You have valid inputs. Proceed...
TextBox3.Text = String.Join(",", Arr1.Select(Function(x) CInt(x)).
Zip(Arr2.Select(Function(x) CInt(x)),
Function(x, y)
Select Case opr
Case "-"c
Return x - y
Case "*"c
Return x * y
Case "/"c
Return x \ y
Case Else
Return x + y
End Select
End Function))
Better yet, enum the arithmetic operations:
Public Enum ArithmeticOperations
Add
Subtract
Multiply
Divide
End Enum
.. and create a parameterized Function that returns the joined string.
'You name it...
Public Function GetCalcString(input1 As String,
input2 As String,
opr As ArithmeticOperations) As String
Dim Arr1() = input1.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
Dim Arr2() = input2.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
If Arr1.Length <> Arr2.Length OrElse
Arr1.Any(Function(x) Not Integer.TryParse(x, Nothing)) OrElse
Arr2.Any(Function(x) Not Integer.TryParse(x, Nothing)) Then
Return Nothing
End If
Return String.Join(",", Arr1.Select(Function(x) CInt(x)).
Zip(Arr2.Select(Function(x) CInt(x)),
Function(x, y)
Select Case opr
Case ArithmeticOperation.Subtract
Return x - y
Case ArithmeticOperation.Multiply
Return x * y
Case ArithmeticOperation.Divide
Return x \ y
Case Else
Return x + y
End Select
End Function))
End Function
Assuming that, the arithmetic operations are listed in a ComboBox in the same order:
Private Sub TheCaller()
TextBox3.Text = GetCalcString(TextBox1.Text,
TextBox12.Text,
CType(ComboBox1.SelectedIndex, ArithmeticOperations))
End Sub
Related
I want to do is just like string join with three text box all line will be merge here's my sample
Textbox1.Text lines:
Sample1
Sample2
Sample3
Textbox2.Text lines:
: (seperator)
Textbox3 lines:
Pass1
Pass2
Pass3
Here's my code for merging all text box
For Each str As String In Me.right.Lines
For Each str2 As String In Me.sep.Text
For Each str3 As String In Me.left.Lines
If Not str2 = "" Then
'(change into list of string)
sam.Text += str + str2 + str3 & vbNewLine
Else
'(change into list of string)
sam.Text += str + str3 & vbNewLine
End If
Next
Next
Next
and I want to do my merge result will be
Dim listsample = New List(Of String)
listsample.AddRange(Strings.Split(str + st2 + st3 + " ", " ", -1, CompareMethod.Binary))
and I want final result will be
listsample.AddRamge(Strings.Split("Sample1:Pass1 Sample2:Pass2 Sample3:Pass3, " ", -1, CompareMethod.Binary))
for easy to understand what I'm saying is how can I convert this
sam.Text += str + str3 & vbNewLine
into this
listsample.AddRamge(Strings.Split("Sample1:Pass1 Sample2:Pass2 Sample3:Pass3, " ", -1, CompareMethod.Binary))
Private Sub OpCode()
Dim lst As New List(Of String)
Dim sep = TextBox2.Text
For Each line1 As String In TextBox1.Lines
For Each line2 As String In TextBox3.Lines
If Not sep = "" Then
lst.Add(line1 & sep & line2)
Else
lst.Add(line1 & line2 & vbNewLine)
End If
Next
Next
For Each s In lst
Debug.Print(s)
Next
End Sub
I have code that parses out the last word on a string.
ie. Stack/Over/Flow will give me "Flow".
But I want to get "Over/Flow".
This is what I got, but only able to get "Flow"
arr(counter - 2) = "'" & mid(Text, InStrRev(Text, "/") + 1) & "'"
I would use Split()
Sub lastTwo()
Dim str As String
str = "Stack/Over/Flow"
Dim splt() As String
splt = Split(str, "/")
If UBound(splt) > 0 Then
Debug.Print splt(UBound(splt) - 1) & "/" & splt(UBound(splt))
End If
End Sub
Here is a function that does it:
Function lastParts(str As String, delim As String, x As Long) As String
Dim splt() As String
splt = Split(str, "/")
If UBound(splt) + 1 >= x Then
Dim t As String
t = "=INDEX(INDEX({""" & Join(splt, """;""") & """},N(IF({1},ROW(" & UBound(splt) - x + 2 & ":" & UBound(splt) + 1 & "))),),)"
lastParts = Join(Application.Transpose(Application.Evaluate(t)), delim)
Else
lastParts = str
End If
End Function
It has three parts, the string, the delimiter and the number of returns.
It can be called using your code:
arr(counter-2) = lastParts(Text,"/",2)
or from the worksheet
=lastParts(A1,"/",2)
Initially misread the question. You can nest InStrRev() calls
arr(counter - 2) = "'" & mid(Text, InStrRev(Text, "/",InStrRev(Text, "/")-1)+1) & "'"
Trim (in VBA for MS-Access 2010) does not remove vbCrLfs, only spaces. In the immediate window, I get
? Len(vbCrLf & "a" & vbCrLf & "b" & vbCrLf)
8
? Len(Trim(vbCrLf & "a" & vbCrLf & "b" & vbCrLf))
8
For spaces however:
? Len(" " & "a" & " " & "b" & " ")
5
? Len(Trim(" " & "a" & " " & "b" & " "))
3
How to make a trim that removes vbCrLFs on the ends only?
If you don't mind removing ALL new lines (and not just edges) you could just do:
myStr = Application.clean(Application.trim(myStr))
For imitating Trim function, you'd need to test each character in your string's edges:
Function TrimNewLines(mtStr As String) As String
Dim pattern As String, c As String
Dim i As Integer: i = 1
pattern = "[" & Chr(10) & Chr(13) & "]"
c = Mid(mtStr, i, 1)
Do While c Like pattern
i = i + 1
c = Mid(mtStr, i, 1)
Loop
mtStr = Mid(mtStr, i, Len(mtStr))
i = Len(mtStr)
c = Mid(mtStr, i, 1)
Do While c Like pattern
i = i - 1
c = Mid(mtStr, i, 1)
Loop
mtStr = Mid(mtStr, 1, i)
TrimNewLines = mtStr
End Function
This seems to have done the trick:
Public Function trimNewlinesAndSpaces(chaine As String) As String
chaine = Trim(chaine)
Do While (left(chaine, 2) = vbCrLf) Or right(chaine, 2) = vbCrLf
If left(chaine, 2) = vbCrLf Then
chaine = right(chaine, Len(chaine) - 2)
End Ifj
If right(chaine, 2) = vbCrLf Then
chaine = left(chaine, Len(chaine) - 2)
End If
chaine = Trim(chaine)
Loop
trimNewlinesAndSpaces = chaine
End Function
I have used this loop to display Data that graph using the Google Chart
The problem is that the graph contains a lot of data and it takes a long time to be made loaning
The question is whether there is a faster way to pass the data base?
dRow As Data.DataRow In xdata.Rows
If j = 0 Then
TempDate = Format(CDate(dRow.Item(0)), "dd/MM/yyyy")
MyXML += "[new Date(" & TempDate.Year & "," & TempDate.Month - 1 & ")"
j += 1
Else
TempDate = Format(CDate(dRow.Item(0)), "dd/MM/yyyy")
MyXML += ",[new Date(" & TempDate.Year & "," & TempDate.Month - 1 & ")"
j += 1
End If
For colIdx As Byte = 1 To xdata.Columns.Count - 1
If colIdx > 0 Then
If (dRow.Item(colIdx)).ToString <> Nothing Then
MyXML += "," & (dRow.Item(colIdx)).ToString
Else
MyXML += "," & "0"
End If
End If
`
You should be using a StringBuilder if you are going to be concatenating hundreds of strings. .NET strings are immutable which means each time they change, a new object has to be created.
Dim sb As New StringBuilder
For dRow As Data.DataRow In xdata.Rows
If j = 0 Then
TempDate = Format(CDate(dRow.Item(0)), "dd/MM/yyyy")
sb.Append("[new Date(" & TempDate.Year & "," & TempDate.Month - 1 & ")")
j += 1
Else
TempDate = Format(CDate(dRow.Item(0)), "dd/MM/yyyy")
sb.Append(",[new Date(" & TempDate.Year & "," & TempDate.Month - 1 & ")")
j += 1
End If
For colIdx As Byte = 1 To xdata.Columns.Count - 1
If colIdx > 0 Then
If (dRow.Item(colIdx)).ToString <> Nothing Then
sb.Append("," & (dRow.Item(colIdx)).ToString)
Else
sb.Append("," & "0")
End If
End If
Next
Next
And when you're done parsing the data:
Return sb.ToString
I'm a graphic artist, new to Excel and VBA but trying to use it to process mountains of data in excel to be used as variable data in Illustrator.
If I want to convert cells with product names for signs like "Budwieser, Bud Light & Bud Black Crown" to an abbreviation following the format "Budweiser_BL_BBC"
I have written a function that I thought would accomplish my task but it returns #VALUE!
Edit
To explain the logic: my idea was to take the string, split it on " & " and then split the first position of the resulting array on ", " then adding what was after the "&" to the end of the second array - this array, sProd, has the products separated into different positions of the array.
Then looping through that array and splitting each product at the spaces creating a jagged array.
Then loop through that array again creating a string taking only the first letter of each word in each product, separating products with an underscore. The exception being that the first word of the first product is spelled out and set in proper case. (Just saw an error in my logic and added the code for the first word exception).
Edit #2
The function should return a string with the first word of the original string set in proper case with all other words abbreviated to their first letter and products separated by underscores. So "Budweiser, Bud Light & Bud Light Lime" returns "Budweiser_BL_BLL", "All Coke & Dr Pepper Products" would return "AllC_DPP" and "Gatorade" returns "Gatorade".
This is my first bout with Excel and VBA.
Function Abbrev(p As String) As String
Dim sAmpersand() As Variant
Dim sProd() As Variant
sAmpersand = Split(p, " & ")
sProd = Split(sAmpersand(0), ", ")
sProd(UBound(sProd)) = sAmpersand(1)
Dim ProductCount As Integer
Dim ProductEnd As Integer
ProductEnd = UBound(sProd) - 1
For ProductCount = 0 To ProductEnd
sProd(ProductCount) = Split(sProd(ProductCount), " ")
ProductCount = ProductCount + 1
Next ProductCount
Dim WordCount As Integer
Dim WordEnd As Integer
WordEnd = UBound(sProd(ProductCount)) - 1
Abbrev = StrConv(sProd(0)(0), vbProperCase)
For ProductCount = 0 To ProductEnd
For WordCount = 0 To WordEnd
If ProductCount = 0 Then
WordCount = 1
End If
Abbrev = Abbrev & Left(sProd(ProductCount)(WordCount), 1)
WordCount = WordCount + 1
Next WordCount
If ProductCount + 1 < ProductEnd Then
Abbrev = Abbrev & "_"
End If
ProductCount = ProductCount + 1
Next ProductCount
End Function
Working code:
Function Abbrev(p As String) As String
Dim res As String, w1, w2
res = Split(Split(p, ",")(0), " ")(0)
If res = Split(p, ",")(0) Then res = res & "_"
For Each w1 In Split(Mid(Replace(p, " &", ","), Len(res) + 1), ",")
For Each w2 In Split(w1, " ")
res = res & Left(w2, 1)
Next w2
res = res & "_"
Next w1
Abbrev = IIf(Right(res, 1) <> "_", res, Left(res, Len(res) - 1))
End Function
Here's a better abbreviate function:
Function Abbreviate(Name As String) As String
Dim I As Integer
Dim sResult As String
Dim sTemp As String
I = InStr(Name, " ")
If I < 1 Then
Abbreviate = Name
Exit Function
End If
sResult = Left$(Name, I)
sTemp = Name
Do While I > 0
sTemp = Right$(sTemp, Len(sTemp) - I)
If Left$(sTemp, 1) = "(" Then
If Mid$(sTemp & "***", 3, 1) = ")" Then
sResult = sResult & " " & Left$(sTemp, 3)
Else
sResult = sResult & " " & Left$(sTemp, 1)
End If
Else
sResult = sResult & " " & Left(sTemp, 1)
End If
I = InStr(sTemp, " ")
Loop
Abbreviate = sResult
End Function
This is from user al_b_cnu on mrexcel.com
Here is a modified version to shorten up the result a bit:
Function Abbreviate(Name As String) As String
Dim I As Integer
Dim sResult As String
Dim sTemp As String
I = InStr(Name, " ")
If I < 1 Then
Abbreviate = Name
Exit Function
End If
sResult = Left$(Name, I)
sTemp = Name
Do While I > 0
sTemp = Right$(sTemp, Len(sTemp) - I)
If Left$(sTemp, 1) = "(" Then
If Mid$(sTemp & "***", 3, 1) = ")" Then
sResult = sResult & Left$(sTemp, 3)
Else
sResult = sResult & Left$(sTemp, 1)
End If
Else
sResult = sResult & Left(sTemp, 1)
End If
I = InStr(sTemp, " ")
Loop
Abbreviate = sResult
End Function