I have a program that gives different outputs depending on if I debug with F10 or simply run the program. Even with F5 from one point to another returns the "weird" output. But if I debug it step by step it gives the correct output.
The project contains some commands that I send to a function generator and, depending on what returns, it shows one picture or another.
I thought that the problem could be that the function generator would need more time to send and receive the data, but I made it 10x longer and the problem doesn't change.
What can cause this problem? Maybe something in the cache or it's definitely my code's problem?
The code where the problem happens:
result = ""
Do Until result <> ""
SendToMachine("SOURCE2:APPL" & vbLf, result)
If result = "" Then GoTo finish
Select Case result
Case ....
End Select
finish:
Loop
Normally the first loop, result gives nothing because it always fails, but the second time if i do it with F10 it works perfectly. It returns the value that is supposed to return and after that the code works perfectly.
When i dont use F10 it shows some rare values.
Maybe the problem is using Goto?.
Related
Sometimes, I want to manually put the execution point to a certain line.
Because I don't want it to normally happen, but only when I put the execution point to this line manually, I name it this way:
If 0 Then
_SomeValue = "This can pnly be hit by setting the execution here manually"
End If
In VB6, it worked. It was one of my favorite ways to do some things.
In VB.NET, I am unable to do put the executor to this line.
Each time I do, it is set to the next line.
Is there any way to do what I want?
I got the suggestion to try If False Then, but it doesn't work for me:
In VB.net, I am sending SCPI commands via GPIB-to-USB to an agilent/keysight device. I am using this sample to write data to instruments. https://www.keysight.com/main/editorial.jspx?cc=BR&lc=por&ckey=492255&nid=-33170.1090751.08&id=492255
Here is a portion of my code that I am having trouble with.
Dim P As Integer
Dim Power As Integer
For P = -50 To 0 Step 10
System.Threading.Thread.Sleep(500)
Power = P
instrument.WriteString("POW" & Str$(P) & "dbm")
Console.WriteLine("Power" & P) 'Testing if this portion of the for loop is reached
My code builds successfully, but the device I am communicating with displays an error message saying "invalid separator." I believe the problem is that P starts as a negative number. If I were to do something like, For P=0 to 10 step 1, it would work on the device receiving the command. However, Str$ does not seem to be work with negative numbers. Using Console.WriteLine, I do see that my loop is working correctly in the console output. Based on the console output, the structure of the loop is not the problem.
How could I make this work with P starting as a negative number? I tested the code and device to see if it can take negative numbers by simply using instrument.WriteString("POW -5 dbm") and that works. I really believe the problem involves Str$. I also tried doing ("POW" & P.ToString() & "dbm") but that did not work either. I also replaced Str$ with CStr but that did not work either. Also note that the sleep/delay is not an issue because I implemented that in another for loop and it worked with no errors.
I just figured it out. There has to be a space after POW. It should look like "POW "
We have an Excel-file with a large amount of VBA behind it. The Excel-file works just fine on my computer, but so far 3 of my colleagues (non-IT'ers) have gotten this error:
Runtime error 5:
Invalid procedure call or argument
The error is located on this line, and I don't see why it is throwing an error there because it's just a simple Set (and it works perfectly fine on my computer):
Set MyButton = Application.CommandBars("Attributions").Controls.Add(Type:=msoControlButton, Before:=10)
We all have the exact same Excel-file. I even sent them my version of the file, in which everything is running fine and no errors happen, but even when they open my version of the file they still get the above error on the above line!
What exactly could cause this? We all have Office 2013 and updates are installed automatically. The problem started about 2 weeks ago with one colleague and since this week I heard from two other colleagues that they have the same problem. One even said it suddenly worked again after he moved to a different desk (which I doubt would have an influence) but shortly after, it started getting the error again.
I have absolutely no idea why they get the error, or what might cause it. Seeing as we now all have the same version and they still get the error, I am thinking it might have something to do with Excel itself but that's just my idea.
Does this sound familiar to anyone here? Or does anyone know what might cause this, and how it can be fixed?
Edit: a while ago I checked on my colleagues their computers to see if the CommandBar was present, and it was. Even then the error still happened.
I suggest you to use a function that checks if there is a CommandBar in your Application like this:
Function IsCommandBarValid(cbName As String) As Boolean
Dim i As Long
IsCommandBarValid = True
For i = 1 To Application.CommandBars.Count
If (Application.CommandBars(i).Name = cbName) Then
Exit Function
End If
Next i
IsCommandBarValid = False
End Function
Now, You can use it to see that your user have that CommandBar in his or her Application, then make it like this:
If (Not IsCommandBarValid("Attributions")) Then
Call Application.CommandBars.Add(Name:="Attributions")
End If
' And after this add your code
Set MyButton = Application.CommandBars("Attributions").Controls.Add(Type:=msoControlButton, Before:=10)
Invalid procedure call or argument (Error 5)
Some part of the call can't be completed. This error has the following causes and solutions:
An argument probably exceeds the range of permitted values. For example, the Sin function can only accept values within a certain range. Positive arguments less than 2,147,483,648 are accepted, while 2,147,483,648 generates this error.
Check the ranges permitted for arguments.
This error can also occur if an attempt is made to call a procedure that isn't valid on the current platform. For example, some procedures may only be valid for Microsoft Windows, or for the Macintosh, and so on.
Check platform-specific information about the procedure.
For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).
MSDN Source Article
The Problem
I am trying to debug some code, and somewhere in the middle I stopped at a breakpoint. Now I want to change some variables and run a certain loop several times.
How far did I get?
I know how to change the variables, but somehow I get stuck when trying to run the loop in the immediate window. Here is an example:
Dim i As Integer
Dim j As Integer
For i = 0 To 6
j=i ' Do something
Next i
I tried several variations of the code, but each time I get the following error:
Compile error: Next without for
Other relevant information
I tried searching but mostly found information about problems with loops, whilst I am quite sure the loop itself is fine. (Especially as I reached it before arriving at the breakpoint).
The only place I saw someone addres this situation, he reduced the loop to a single line, however to do this every time would be very impractical in my case.
I realize that I could call a function containing the loop, and then the function call would probably work, but again this feels quite impractical. So I guess it boils down to the following question.
The question
What is a practical way to run a loop whilst debugging VBA code in Excel?
There is actually a way for using loops or other multi-line statements in the Immediate Window - using a colon : to separate statements instead of a new line.
Full solution is described here.
Note that in the Immediate Window you also don't have to declare the variables using a Dim statement.
To summarize, your snippet would look something like this:
For i = 0 To 6: j=i: debug.Print i+j: Next i
I think I understand your question. You want to run a multi-line code block (i.e. the loop) in the Immediate Window. This throws errors because the Immediate Window is only intended for single lines of code.
I don't have any suggestions other than those you already mentioned. I'd recommend putting your test loop into a separate function and calling that from the Immediate Window:
Sub Test()
Dim i As Integer
Dim j As Integer
For i = 0 To 6
j=i ' Do something
Next i
End
Another option is to set several breakpoints. You can also run one line of code at a time with F8.
What is likely the preferred method (i.e., what most people actually do) is use the full power of the IDE, which includes the Immediate, Locals and Watch panes. You can change the value of most variables at runtime by direct assignment in the Immediate Pane (i=6 will do exactly what you think it should do). The IDE also allows you to set breakpoints, add watch conditions, step through code line-by-line using the F8, step through function or procedure calls using Shift+F8, stepping over (and back) through code using the mouse/cursor, and with a few exceptions, you can even add new variables during runtime.
I'm having some kind of problem in a piece of code.
I need to find values in a excel book and then copy it to another. The thing is that i need to copy it concept by concept in the right place. The problem is that the file doesnt come every week in the same order.
So i need to find the concept and then copy the next cell that is the value of that concept.
First of all i use need to locate the correct line so i can start copying (this part is easy and its done).
Secondly, having the correct line, i need to find the concept, which in this example im going to use the "619".
After having find the location of this value i store the value in "c_audiovisual".
On Error GoTo ErrhandlerCAV
lRowC_AV = Application.WorksheetFunction.Match(619, Range("A" & line & ":FI" & line), 0) + 1
'On Error GoTo ErrhandlerCAV
Continue:
If errorCAV = 1 Then
c_audiovisual = 0
errorCAV = 0
Else
c_audiovisual = ActiveSheet.Cells(line+ m2, lRowC_AV).Value
End If
I've made a escape in case that this concept isn't present in that line (which sometimes occurs).
Sometimes this piece of code works, and other it doesn't.
When i'm on debug mode (pressing F8) it works. And when i use small files to look for the values it works. On bigger files sometimes don't.
Any ideas?
I faced similar issues. I fixed it with using ThisWorkbook.Sheets("MySheet").Range(...) inside the Match function. Or try ActiveWorkbook as applicable. Let me know if this worked. Curious to know.
Btw I noticed column and row number both are "line". Is that a mistake?