Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am currently creating a chess game a to save me a large amount of if statements I was wondering if there is a way to insert a variable such as x into the label below:
Label1.Text = "MyRedPawns()"
I want it to go in the ( ) similar to what would be used for a Console.Writeline statement.
Edit:
Here is the code:
For x = 1 To 4
If mouseclicklocation.X >= MyRedPawns(x).getposition.X And mouseclicklocation.X <= MyRedPawns(x).getposition.X + 80 Then
If mouseclicklocation.Y >= MyRedPawns(x).getposition.Y And mouseclicklocation.Y <= MyRedPawns(x).getposition.Y + 80 Then
Label1.Text = "MyRedPawns( )"
Piecefound = True
End If
End If
Next x
I want the value of x which is in the for loop to be passed in to the label1.text inside the ()
You can do something like this:
Label1.Text = "MyRedPawns(" & x & ")"
The question is not clear. However, i'll give it a try. I guess you want to insert the value of a variable x into a string. You can use String.Format:
Dim pattern = "MyRedPawns({0})"
Dim xVariable As Int32 = 4711
Dim result = String.Format(pattern, xVariable)
Result: MyRedPawns(4711)
Have a look at the remarks section of MSDN to see how it works.
If we had a variable x = "Hello" to get the text from x into the label we would do:
Label1.Text = x
I'm not sure what MyRedPawns() actually does or returns so I could be wrong but if it is returning a String and takes a parameter then you should be able to do:
Label1.Text = MyRedPawns(x)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
.
Hello guys,I need your inputs on the topic here.
I intend to combine or concatenate different value in different columns so its shown in a single cell.
Illustration is the following:
1:
Is it possible to do this without macro?
use
=CONCATENATE("The name of the painter: ",A3,CHAR(10), "The Hobby: ", B3, CHAR(10), "Tool used: ", C3,CHAR(10),"Remuneration: ", D3)
To answer your second question on how to do it with code:
Sub PopulateResultsToCell()
Dim X As Long, MyArr As Variant, PrefixArr As Variant
PrefixArr = Array("The name of the painter: ", "The Hobby: ", "Tool used: ", "Remuneration: ")
MyArr = Application.Transpose(Application.Transpose(Range("A3:D3"))) '<-- Change this for the range to read
For X = LBound(MyArr) To UBound(MyArr)
MyArr(X) = PrefixArr(X - 1) & Trim(MyArr(X)) 'Note: Option base is zero but transposing creates a base 1 array hence the X minus 1
Next
Range("F3").Formula = Join(MyArr, vbLf) '<-- Change this for where to populate the result to
End Sub
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm new to visual basic and I'm using visual studio 2008 express edition.
I need to create a program to calculate a grade / average.
Do I need to repeat if / then / else statements for each textbox?
Please Refer to the linked image below for reference.
I'm not sure how to average these values.
Would I need to create many, repeated conditional statements?
Try this.
Dim tbList As TextBox() = {txtMath, txtEnglish, txtScience, txtFilipino, txtPE}
Dim Ave As Double = 0
For Each item In tbList
If item.Text = Nothing Then
item.Text = "0"
End If
Ave += Val(item.Text)
Next
Ave = Ave / tbList.Length
lb_Average.Text = Ave.ToString
I would recommend starting with a few if statements, that add 1 point each to a variable IF the textbox has a value. Repeat that statement for each textbox.
Then, add up the values of each textbox.
Finally, divide the sum of the values of each text box by the number of textboxes that contain a value, which you would have counted using the if / then statements earlier.
So, in the calculate button's click event, it may look something like this (Written in vb.net) :
Dim Count as Integer = 0
If txtMath.text <> "" Then Count += 1
If txtEnglish.text <> "" Then Count += 1
If txtScience.text <> "" Then Count += 1
If txtFilipino.text <> "" Then Count += 1
If txtPE.text <> "" Then Count += 1
Dim Sum as decimal = (txtmath.text + txtenglish.text + txtScience.text + txtFilipino.text + txtPE.text) / Count
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table below with details in column A and B. I want to search a string in C from A and paste value of Column B in Column D with help of VBA.
Example:-
A B C D
STRAT Strategy s_strat_nhnh Strategy
TRDMK Trademarks bng_trdm_ndnd Trademarks
TRDMK not in bng_trdm_ndnd.
But if I got it right, you want something like
Then Code is:
Sub Test()
CStartRow = 1
CEndRow = 5
AStartRow = 1
AEndRow = 3
For I = CStartRow To CEndRow
For J = AStartRow To AEndRow
If InStr(UCase(Range("C" + CStr(I))), UCase(Range("A" + CStr(J)))) Then
Range("D" + CStr(I)) = Range("B" + CStr(J))
Exit For
End If
Next J
Next I
End Sub
If you do not want to use VBA then you can use this formula.
Formula in d1 cell is:
=IF(ISNUMBER(SEARCH(A1,C1)),B1,"")
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am confused as how to create interest rate input as an argument
When I try to list Dim Rate as single an error box says duplicate argument
The purpose of this function is to calculate NPV for a set of cash flows (myarray)
Public Function myNPV(rate As Single, r As Range) As Single
Dim myArray(64) As Double
Dim i As Integer
Dim sum As Double
For i = 0 To n
Next i
For Each elem In r
myArray(i) = elem.Value
myArray(i) = myArray(i) / ((1 + rate / 100) ^ i)
i = i + 1
Next elem
End Function
n is zero. The For loop does nothing.
myNPV never gets assigned a value in the UDF
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following string in excel:
" 163,40 3,10 1,86 163,30 163,40 167,00 163,30 435862329"
And I have no problem to split up this column into 8 individual columns - one for each block of data. But I saw that the first column - here 163.40 is truncated so it becomes 163 - that is from a float to an integer. I realized later thats because the numbers is preceeded by four spaces - " 163.40".
So my question is how to delete these four spaces - and ONLY these four first spaces.
That would solve my problem.
Any ideas?
Use Mid function like used below for your problem.
Mid(text, 5, Len(text))
Assuming that your source string is in cell A1 and you need the 8 columns data in row 2 ; please refer to below code :
Function SplitMyData()
Dim var As Variant
var = Split(Trim(Range("A1").Value), " ", , vbTextCompare)
For i = 0 To UBound(var)
Cells(2, i + 1).Value = var(i) 'Pasting vals in row 2
Next
End Function
You can change the source and destination cell references as per your requirements. :)