This question already has answers here:
VBA power operator (^) not working as expected in 64-bit VBA [duplicate]
(2 answers)
Closed 2 years ago.
Today Visio VBA is behaving differently. debug.print 2^2 prints "2 2" and debug.print 5.5^2 gives an error message "Compile error: Expected expression" while debug.print (5.5)^2 gives 30.25.
I don't remember ever seeing this before in the past probably 15 years of VBA programming, I expected to get 4 and 30.25
Use 2 ^ 2. The space is important.
Related
This question already has an answer here:
VBA - Sub or Function not Defined
(1 answer)
Closed 2 years ago.
Why I am getting an compilation error: "Sub or Function no defined" ?
Public Function generateCode() As String
generateCode = RandBetween(3, 12)
End Function
RandBetween is not a build-in function in VBA, it is an Excel function. As long as you are programming for Excel, you can access Excel functions via WorksheetFunction, in your case Application.WorksheetFunction.RandBetween(3, 12)
This question already has answers here:
VB6 pass by value and pass by reference
(2 answers)
Closed 1 year ago.
Public Sub SavePendleValues(ByVal row1 As Integer, ByVal row2 As Integer)
Calling it
For sheetrow = 2 To 15 ' number of rows to scan
SavePendleValues (sheetrow, sheetrow)
Next sheetrow
Getting error : Compile error: Syntax error
When I uncomment the line: SavePendleValues (sheetrow, sheetrow)
Everything works.
Remove the parentheses.
SavePendleValues sheetrow, sheetrow
Otherwise you are trying to pass as a first argument something in parentheses that has two variables in it, which doesn't make sense for VBA parser.
I kept getting this compile error syntax error, I copied code from here: https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2007/bb206765(v=office.12)?redirectedfrom=MSDN
So the code should be good.
Then, somehow, I removed all the leading empty spaces for all lines, the error goes away!
So it seems, the vba editor is not very tolerant, it doesn't like certain invisible printing-control characters.
This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
IndexOutOfRangeException in VB.NET
(2 answers)
What does IndexOutofRangeException mean?
(3 answers)
“IndexOutOfRangeException was unhandled” [duplicate]
(2 answers)
Closed 4 years ago.
I have this code:
Module Module11
Sub Main()
Dim mess As String
Dim out As String
Dim num, p As UInteger
Dim q As Integer = -1
Console.Write("Enter a message: ")
mess = Console.ReadLine()
While p < 9
q += 1
num = Asc(mess(p + q)) + 5
out = Chr(num)
Console.Write(out)
End While
Console.ReadKey()
End Sub
End Module
But on Line num = Asc(mess(p + q)) + 5, it is showing 'index out of the bounds of array'.
I was actually trying to create code that could change every character (even blank space) to the next sixth character (with reference to ASCII codes) of whatever character we input.
It shows error even after giving the correct output (in the black console).
Please help.
I think I have the answer (or at least AN answer). I'm not familiar with visual basic, but I am familiar with Python and this seems to be extremely similar. After doing some quick researching, I learned that the "UInteger" data type cannot be negative. I would recommend changing the while loop to read "While p > 0 And p < 9" and then the rest of the code. It looks to me like your answer is coming out negative. This would make sense considering what your error message says. However, I'm not sure how the output would still come out correctly, but it's worth a shot. I would try that, and let me know how it goes!
This question already has answers here:
Decimal TryParse in VBA
(2 answers)
Closed 4 years ago.
I made a simple vba application that allows a user to enter data into excel. The data that's being entered had to be >= 1.99 ohms and can be 3.00 or higher. Everything works great and it's been in use for over a year now. However, recently a user fat fingered a decimal point and didn't catch it until they entered the number. VBA didn't like that and errored out. The format they entered looked like so: '.2.34'. I can't seem to figure out how to avoid this. Any suggestions?
I've tried using:
If Res.value <> Format(Number, "#.##") Then
Msgbox ("blah blah")
Res.Value = ""
Exit Sub
End if
This blocks those entries, but it also blocks correct entries.
Maybe you could add Data Validation rules on the typical ranges. Try this: Data > Data Tools > Data Validation
This question already has answers here:
IF statements in VBA
(5 answers)
Closed 7 years ago.
see the code below.
IF Weight=0 Then CallFunction
IF Weight=100 Then
Heavy=True
Else
Heavy= False
End If
How is the program working when I have not ended a if statement?
its VBA's functionality to end up if statements in a single line..
If Weight is equal to zero, then CallFunction is executed (if Weight is not equal to zero, a CallFunction is not executed), then the code is going to another if statement. That's all! Debug the program to find out ;)