I got a knew problem which should be the last one from my bonus exercise. At first let's explain the rules, I have a word which is shuffle. I want my user to find this word and he has a determined number of tries.
Now I want to let the user know which char from his input (txtBox.Text) was right and which one aren't. So I tried to create a method which is able to Color a char in green If the char is correctly positionned else in red. I ask my teacher and it seems to be a hard thing to do, I tried to find the solution he gave me with richBox but it's way too hard right now, i'm struggling way to much.
So ! I think of something way simpler, or at least i thought it was simpler, I get the user input in a String and I do a loop around it, every time the correct word and the input char doesn't match I replaced it by a dot.
It doesen't work either if I try to put an index to my :
If word(i) IsNot proposition(j) Then
I'm facing an out of bound and if I try without it, it consider my string as an array of 1 and add to my listbox a single dot.
Here's my code :
Public Sub charRight&Wrong()
Dim proposition() As String = {txtInput.Text} //get the proposition from the user
Dim dot As Char = "."
Dim word() As String = {theWord} //theWord represent the right answer
For i = 0 To theWord.Length - 1
For j = 0 To proposition.Length - 1
If word(i) IsNot proposition(j) Then
proposition(j) = dot
End If
Next
Next
lboAttempts.Items.AddRange(proposition.ToArray)
End Sub
I don't really know where I'm wrong I hope you can point it out. Thanks again.
ThisWorkbook.Names("numWorkersCompEntries").Value = ThisWorkbook.Names("numWorkersCompEntries").Value + 1
I would like to store an incrementing variable in the name manager rather than storing it somewhere in the spreadsheet so that I can manipulate it by name. The above code seems intuitive to me but I am getting a type mismatch. Any ideas? Thanks for any help you can provide.
--Drake
If you modify your code a bit, you'll see that the values of a single numeric are actually stored as a string, so you're trying to add a string, which Excel isn't happy about. You can try something like this:
Sub test()
Dim namedValue As Variant
namedValue = ThisWorkbook.Names("Foo").Value
ThisWorkbook.Names("Foo").Value = Mid(namedValue, 2, Len(namedValue)) + 1
Debug.Print ThisWorkbook.Names("Foo").Value
End Sub
Which seemed to have the expected results on my test sheet...just make sure you have a named range of "Foo" to test. Obviously tweak to suit your situation, but hopefully it will get you started in the right direction.
So I looked through other user's question but couldn't find on specifically upon what I am looking for. What I am trying to do is very simple. I am writing a Visual Studio Macro and am trying to obtain the number of the current line that the TextSelection is on. So that's it really, my question is quite simple. How do you get the number of the line that the selection is currently at? Any help is greatly appreciated!
Just so it's clear to anybody reading this, I am using VB and am writing Visual Studio Macro.
Keep in mind that a TextSelection can span multiple lines, so there's potentially a range of lines.
Looking at the docs for TextSelection (i.e. I haven't tested this), you should be able to do something like this:
Dim mySelection As EnvDTE.TextSelection = ' however you get the active selection
mySelection.TopPoint.Line ' gets the line of the top of the selection
If you want to get it based on where the cursor is (top or bottom of the selection) you can try this:
mySelection.ActivePoint.Line
It looks like the TextRanges might also be useful, but it sounds like it's for box selection only, so it might not apply.
mySelection.TextRanges.Item(0).Line
There is probably a better way than this, but the first way that comes to my mind is something like this.
First, make sure that the last line in your file is "xyz"
Dim linenumber As Integer = 1
dim mystring as string = ""
Using myfile As New IO.StreamReader("C:/myfile")
mystring = myfile.readline()
while mystring <> "xyz"
linenumber += 1
messagebox.Show(mystring & " is on line " & linenumber)
end while
End Using
So if the contents of C:/myfile looked like this....
I
Love
Pie
Then you would get as output....
"I is on line 1"
"Love is on line 2"
"Pie is on line 3"
I'm currently trying to add a hyperlink to a web url in word through a VB program. I'm stumbling around to try and find the proper syntax and what I need to accomplish this, because I've been getting a lot of unhelpful VBA examples, which is not what I need at all.
My code looks like this:
sPara2 = oDoc.Content.Paragraphs.Add
sPara2.Range.Text = attachmentRdr("attachmentName")
sPara2.Range.Hyperlinks.Add(attachmentRdr("attachmentPath"))
sPara2.Format.SpaceAfter = 24 '24 pt spacing after paragraph.
sPara2.Range.InsertParagraphAfter()
where attachmentRdr is a sqlDatareader reading strings of text (attachment name, and path) from a database. If I run this, I get an error for bad parameters (which gets' proced off of the hyperlinks.add()).
Pass range through as the first parameter to the Add function, followed by your URL:
Dim range As Microsoft.Office.Interop.Word.Range
range = Me.Application.Selection.Range
range.Hyperlinks.Add(range, "http://www.microsoft.com")
I've encountered a very strange bug in VBA and wondered if anyone could shed some light?
I'm calling a worksheet function like this:
Dim lMyRow As Long
lMyRow = WorksheetFunction.Match(vItemID, rngMyRange.Columns(1), 0)
This is intended to get the row of the item I pass in. Under certain circumstances (although I can't pin down exactly when), odd things happen to the call to the Match function.
If I execute that line in the immediate window, I get the following:
lMyRow = WorksheetFunction.Match(vItemID, rngMyRange.Columns(1), 0)
?lMyRow
10
i.e. the lookup works, and lMyRow gets a value assigned to it. If I let that statement execute in the actual code, I lMyRow gets a value of 0.
This seems very odd! I don't understand how executing something in the immediate window can succeed in assigning a value, where the same call, at the same point in program execution can give a value of 0 when it runs normally in code!
The only thing I can think of is that it's some odd casting thing, but I get the same behaviour taking if the variable to which I'm assigning is an int, a double, or even a string.
I don't even know where to begin with this - help!!
The only difference between the immediate window and normal code run is the scope.
Code in the immediate window runs in the current application scope.
If nothing is currently running this means a global scope.
The code when put in a VBA function is restricted to the function scope.
So my guess is that one of your variables is out of scope.
I would put a breakpoint in your function on that line and add watches to find out which variable is not set.
And if you don't have Option Explicit at the top of your vba code module, you should add it.
You're not assigning the function name so that function will always return zero (if you're expecting a Long). It seems you should have
makeTheLookup = lMyRow
at the end of your function.
I don't know if you are still looking at this or not but I would have written it this way:
Function makeTheLookup(vItemID As Variant, rngMyRange as Range)as Long
makeTheLookUp = WorksheetFunction.Match(vItemID, rngMyRange.Columns(1), 0)
End Function
I cannot reproduce the problem with Excel 2007.
This was the code I used:
Sub test()
Dim vItemID As Variant
Dim lMyRow As Long
Dim rngMyRange As Range
Set rngMyRange = ActiveWorkbook.Sheets(1).Range("A1:Z256")
vItemID = 8
lMyRow = WorksheetFunction.Match(vItemID, rngMyRange.Columns(1), 0)
Debug.Print lMyRow
End Sub
It may sound stupid but are you sure that all parameters of the Match function are the same in your macro and in the immediate window? Maybe the range object has changed?
Thanks for the answers guys - I should have been slightly more specific in the way I'm making the call below:
Function makeTheLookup(vItemID As Variant, rngMyRange as Range)
Dim lMyRow As Long
lMyRow = WorksheetFunction.Match(vItemID, rngMyRange.Columns(1), 0)
End Function
The odd thing is, I'm passing the two parameters into the function so I can't see any way they could be different inside and outside of the function. That said, I'm still entirely clueless as to what's causing this, particularly since it's a really intermittent problem
Is there any easy way of comparing the range object in the function context to the range object in the Immediate window context and telling if they're different? Given that range is a reference type, it feels like I should just be able to compare two pointers, but I've got no idea how to do that in VBA!
I'm using Excel 2007 by the way, although I'm not sure if that makes any difference.
As will has mentioned above, It most definitely seems like a problem of scope. Or an Obscure Bug.
I would try to use Debug.print statements, as well as Watch, and see if they match up, and take it from there.