VBA Excel - Macscript causing problems on windows when using IIF - excel

I'm creating a macro that opens a file that everyone has on their computer and in order to do so must know the person's username / work ID.
To get the person's work ID I've tried using the following:
sso = IIf(InStr(Application.OperatingSystem, "Windows") = 1, Environ("UserName"), _
'MacScript("(user name as string)"))
Running this on windows returns an error because of the Macscript (I think) and I'd assume the same would happen vice versa, even though the error part of the IIF is never actually accessed I'm guessing seeing as the whole line is executed this is why there is a problem, thus On Error Resume Next would not really help here.
I know this can be easily overcome by just using an if and else statement but I just want to know if I'm right / why this problem occurs and if there are any other more sophisticated ways of achieving what I want.
Thanks

The IIF function evaluates both the true and false parts, or rather it attempts to do so. There is no short-circuit. Your assumption about why it's failing (and also that you can't use an OERN) is correct. You may take a look at conditional compilation logic, if certain parts of your code will not compile on Windows (or Mac, respectively).
http://msdn.microsoft.com/en-us/library/aa240847(v=vs.60).aspx

Related

Excel VBA objsnmp.set strange things

I use the objSNMP.get method in Excel VBA without any problems.
I'd like to use the objSNMP.set method, but unfortunately it's not that easy. According to the website, it should work similarly to get, with the difference that there is one more parameter: the value to be sent.
If I try the official way:
objSNMP.Set ("43.18.1.1.2", OIDValue)
Image1
I get the message "Compile error: Syntax error".
I found another solution that works conditionally. Namely as follows (it can be seen commented out in the picture):
randomVarName = objSNMP.Set("OID", Value)
For example:
temp = objSNMP.Set(".1.3.6.1.4.1.9.9.68.1.2.2.1.2." & PortNum, 21)
In this case, the code runs without error. This is interesting because I haven't found any official information about this anywhere. Somewhere deep in the recesses of the internet, I only found this possible solution some time ago.
If, on the other hand, I do not enter the value directly, but write the name of a variable there (e.g. VLANNum),
temp = objSNMP.Set(".1.3.6.1.4.1.9.9.68.1.2.2.1.2." & PortNum, VLANNum)
I receive an error message. Image2
It doesn't matter if the type of the variable is not declared, string or integer. I also tried several different cell types in Excel, but nothing changed.
The error message is:
Run-time error '-2147467259 (80004005)':
The requested SNMP operation attempted to modify a variable, but
either a syntax or value error occurred.
Based on the above, I cannot insert the value read from the excel table at the end of the "objSNMP.Set" method in such a way that it can send the value. I could only solve the task if I create 4094 different "objSNMP.Set" lines and select what is necessary from among them. Not very efficient.
I have the solution. The value transferred with the variable works in the following form:
objSNMP.Set ".1.3.6.1.2.1.2.2.1.7.9", CInt(x)
Where x is the variable.

Why doesn't my excel vba if/or statement work correctly?

I'm comparing values of numbers from 2 data sheets, and I've dropped the relevant data from both into their own arrays. I need to find matching values to run other steps of analysis.
For i = Lbound(Array1) to UBound(Array1)
For j = LBound(Array2) to UBound(Array2)
If (criteria for Array2) then
variable = 11111
Else
variable = 22222
End if
If variable = Array1(i,1) Or variable = Array1(i,2) or variable = Array1(i,3) then
more steps
Else
more steps
End if
next j
next i
The first if statement sets the variable correctly, but the variable doesn't match any of the criteria. It doesn't go to the else like it should. Now I only know this because I walked through the code step by step. If I just F5 and run the thing, "Excel is not responding". I don't know what the hang up it. All of my variables are declared and assigned a type, I'm not missing any closing statements. And I have no idea what I'm doing wrong.
What do I need to check for in my code?
EDIT
Sorry, but in this instance I'm not allowed to upload any code here. It's work related, NDA kind of stuff. Hence the pseudo code. What I need to show wouldn't be a big deal(at least I don't think it would), but I'm not risking it.
My apologies.
The solution, as it turns out, has to do with a poorly named array(not me) and a simple typo(definitely me). I'm certain that would have been an easy solve for the good citizens of Stack Overflow if I would have been allowed to post actual code.
For what's it worth, I think it's dumb that I couldn't in this case. Thanks #ScottCraner and #SuperSymmertry for trying to be helpful even without much to go on.
Super, I'm still curious about Val. If you've got a minute, I would appreciate more knowledge on that. Anything from an actual person is better than Microsoft documentation.

return value from select case

How can I get the value from the expression in my Select Case statement?
When we write select case statements, we are told that good practice is to have a case else at the end. I understand the purpose of this to be if we don't think of all possible cases, there is a way to inform us instead of just moving on. Usually I just use debug.assert in that case, which works great for personal debugging and is sometimes sufficient for some end users after delivery.
That doesn't work well when I am writing to the VBE, as breaking is not supported anytime after the VBE has been called. I realize that I could probably unload the VBE object and then debug.assert, but that kind of defeats the purpose of pausing my code, if the case has to do with what I am reading/writing to the VBE.
To my mind, the easiest solution would be to msgbox TheUnexpectedResultFromMyExpression, but I have no idea how to call that. The second easiest solution appears to be to have full and complete prescience of what my users may or may not do, as well as when and how they may do it. I have been working on that too, so if you don't know how to return the value, then maybe you have some tips on omniscience.
I know that I, in the vase majority of cases, could simply copy the expression itself to a msgbox ... inside case else, but I happen to be working with a case of a decision tree based upon the return to setting an object, and I am not interested in doing that twice. Another option might be to just myVariable=<expression> and select case myVariable instead of select case <expression>, and then always debug.print myVariable before every select case, but my log is already so busy, doing that in a larger project would mean I have to buy another monitor, and I am struggling with groceries right now.
Asking here seems easier. Thanks.
EDIT:
For those that seemed to have a hard time understanding what I am trying to ask, I boiled the code down as simply as I could. Obviously the below isn't super useful, but you get the idea.
Select Case Forms("Form1").Module.CreateEventProc("Click", Forms("Form1").Controls("label0").Name)
Case 1
Debug.Print "line1"
Case 2
Debug.Print "line2"
Case Else
Debug.Print Forms("Form1").Module.CreateEventProc("Click", Forms("Form1").Controls("label0").Name)
End Select
When my Case Else statement runs, it may print the value of the expression (the workaround I mentioned) in the immediate window, and I know I could do the same with a msgbox or variant or whatever, but it runs the code again. And yes, as I mentioned above already, I could (in this case) just assign the value to a long and then run the Select Case on the long, but that option doesn't solve the problem in my application. And lest this become a conversation where we debate the merits of using VBE objects, or tries to get me to ask a different question of why I get unexpected values (I am not, I am trying to plan for clean debugging during runtime), or someone asking me why I can't just make the code simpler and easier to use instead of tens of thousands of lines of code that write another thousand+ lines of code, it is because I have a client. And they pay for what they want.
So, back to original question, all I want is to know how to return the value from the select case expression. If you are a superhero, and can get code to pause after the VBE is called, then by all means answer that question instead or in addition to.
The answer is: you can't.
Sorry, no source, except multiple descriptions of Select Case, e.g. MSDN, which would mention it, if this functionality existed.
It is a rather unusual question, that's one reason for the confusion in the comments. You are looking for a "meta" variable or method, like ##IDENTITY in T-SQL. But this doesn't exist for the VBA Select Case.
And the reason why it isn't needed: you have complete control over the testexpression in the Select part. The usual way, and I actually consider this good programming practice, is to always assign any sort of complex expression or method call (like CreateEventProc) to a variable, and then use this variable for Select Case.
So
LineNr = Forms("Form1").Module.CreateEventProc("Click", Forms("Form1").Controls("label0").Name)
Select Case LineNr
Case <expected values>
' do something useful
Case Else
Debug.Print "Whoa, unexpected LineNr: " & LineNr
End Select
is really the and the only solution.
I just wrote this maybe itll help?
Private Sub mystuff()
Dim stuff As String
Select Case stuff
Case Is = "Mine"
stuff = "yours "
Case Is = "Not yours"
stuff = "his "
Case Else
stuff = "Hers"
End Select
debug.print "stuff"; stuff
End Sub

Excel VBA not giving error messages in debug mode

I am writing some Macros in Excel and everything was going very well.
At the moment, everything is still sort-of working, but I've encountered a strange problem and I can't find any threads about it.
When the program encounters a code error, such as type mismatch or wrong references (examples below), it does not break the code at the culprit line, but just aborts the entire macro and ends it peacefully with no messages or anything.
It's like I accidentally changed a setting or something that is now causing the debugger to not bug me with debug messages.
Please help! I would like my error messages and "just in time" breaks back!
Dim Margin As Double
If COS <> 0 Then
Margin = Round((SALES - COS) / COS * 100, 2)
Else
Margin = "???"
End If
or like a wrong reference:
Dim that_cell as string
that_cell = "5"
Range(that_cell).Select
Thanks,
Pieka
I've just come across this same issue over the past few days. I've spent copious amounts of time extending Access with VBA and never had an issue quite like this. However, I am aware of the Error trapping options and when to use each option. SO, to my chagrin, to fix this error I had to do the unintuitive opposite of what mehow suggested. I actually changed the Error Trapping option to 'Break on All Errors.' All of sudden, I get the debugging options I'm used to getting.
Now, obviously, once you take a piece of code into production, you'll need to choose 'Break on Unhandled Errors' (and pray you've trapped and planned for every concievalbe error.....).
I've just had a similar problem that was caused by me trying to assign a default value of 0 to an optional range object:
Function test(rng1 as Range, Optional rng2 = 0)
test = "Never gets to this line"
End Function
The function returns a #VALUE error without letting you enter debug mode to find out that it is the function declaration line causing the error.
I know it's a rookie error to try and assign a value to a range object, but if anyone else is struggling to enter debug mode, it might be because you are incorrectly assigning values to objects in your parameter list.

VB6 calls to ActiveX component with string parameters yield weird results

I'm currently finishing a piece of software a now gone co-worker started.
The app is coded in VB6 and uses a 3rd party ActiveX component to act upon a 3rd party system. Our solution is basically an integration between their company's software and ours.
The issue I'm having is that there's a method call that fails consistently, even though it's passed perfectly valid parameters on our side (it's a login method). However, when I look at the trace their application offers, I see that instead of the username I specify, it tells me (roughly) "User '⚠⚠⚠' can't login".
I figured it was likely to be an encoding issue as the ⚠ character replacing the characters I give it to log on seem to be there because the characters are unknown, but nothing I did could fix it.
Anyone know of an issue with VB6 communicating with ActiveX components like this? Or anyone have an idea what I could try? I'm at a loss here and if the issue is on their side, it'll be a pain to get it fixed as we don't have their source code.
Thanks in advance.
There are a couple of ways of passing strings. Aside from the obvious one of passing a string as in
DIM u As String
DIM p As String
u = "Username"
p = "Password"
Set objIRC = objRCL.Login(u, p)
there's also the possibility that .Login is expecting pointers to String, in which case code
Set objIRC = objRCL.Login(StrPtr(u), StrPtr(p))

Resources