Having a look in other threads on the issue, I've found how I can do it, but still i have some issue with my code (printing some values to a file. )
I'm trying to read some data from Excel sheet (it has 32 lines) and print it to a text file in some defined format. The code is for Excel Visual Basic
For y = 1 to 32
Print #1, y.ToString("D2")
Print #1, "some text" & y.ToString("D2")
Next y
I'm getting runtime error 424: object required on both of printing commands.
Am I missing something?
You seem to be mixing up Visual Basic with Visual Basic for Applications (VBA): they are quite different.
In VBA you can format a number with the Format function:
Format(y, "00")
Related
Excel VBA: My problem is simple (as am I!!). I need to use an "Input Box" to input a number (3 digits = xxx) and end up with a goto Linexxx i.e Line is text, xxx is (obviously) variable. Ive tried trying to string the xxx to a Text "Line" using & (ampersand) i.e. goto "Line"&Cont1 (Cont1 is a "variant"), but VBA gives "compile error: Expected Line Number or Label". As I am fairly new to VBA, could someone point me in the right direction, please. Thank you
As far as I can tell, there is nothing in VBA that tells you what line you are on or at what line an error has occurred. Is there some way to get the line number like there is in Visual Basic, ie
Dim CurrentStack As System.Diagnostics.StackTrace
lineNo = CurrentStack.GetFrame(0).GetFileLineNumber
How can one adapt this code for VBA?
erl will give you the error line number but only if you have added line numbers to your code, either manually or using an add-in like MZ Tools.
I have one of the stranges problems I have encountered for many years. I have a Workbook with a lot of code that validates the users data and has been used for many years now. A user reported that the latest version of the file was crashing excel and giving him an "automation error". To my knowledge the changes made to the most recent file were minor and should not have caused this. On analysis the cause of the problem was straight forward, but how it happened, why it happened and how to fix it, I do not know.
The issue occurred due to the below line of code, which is expecting a numeric, but the user supplied a string:
Ltrim(Str(Usersdata(UsersDataRow,UUID_Col)))
Note the upper case S on str. The previous version of the workbook has the same line but str is in all lower case, and does NOT crash excel.
Both files syntax check perfectly. But the strangest thing is when I have both old and new version of the file open in the VBA editor.
If I edit the line in the old file and change str to Str, the editor autocorrects it back to all lower case str.
If I edit the new workbook and change Str to str, the editor autocorrects it back to init cap Str.
So as it stands, I cannot correct the new file.
This behaviour is very strange and am hoping someone can tell me how it has happened and possibly how to solve it?
Regarding fixing the main problem I suggest replacing the line
Ltrim(Str(Usersdata(UsersDataRow,UUID_Col)))
with a more thorough input validation that can handle alphanumeric values
.
As Siddharth Rout suggested in the comments:
The symptoms you describe indicate that the VBA name space has been corrupted
The most common source are variable names like "str", "val", "name", "file", "count", "cell", "row" etc
A quick way to check name conflicts is to click inside the variable name and press F1;
the Help should show "Keyword Not Found"
.
The experiment bellow can demonstrate the problem: open a new Excel, and Alt + F11 for VBA
paste this code in a standard module:
Sub test1()
Dim txt As String 'valid variable name
txt = Str("123") 'Str() remains with a capital S
End Sub
.
Now replace the code with this (obvious problem):
Sub test2()
Dim str As String 'invalid variable name
str = str("123") 'Str() is converted to lower case "s"
End Sub
.
VBA is now corrupted and here is one way to fix it:
close the file
reopen it, and do not allow macros to run
open VBA editor (Alt + F11)
perform a Search and Replace in all VBA modules for "str" (replace "str" with "Str")
Match Case
Find Whole Word Only
Current Project <-- most important setting
The Replace operation will be performed only once, because VBA will automatically convert all other instances of "str" to "Str" before any other replacements
Name space is now restored the next time you open the file
(the procedure forces a recompilation of the P-code generated for all modules)
Another way to re-generate clean P-code is to export each individual standard module as *.bas files, *.cls for Class modules, and *.frm for user forms code, and import all into a new Excel file
I've been trying to find a way to render \r\n as actual newlines in the immediate and command window in my VB.NET 4.5.1 application. I came across this question which teaches about the nq specifier, but it appears to apply only to C#. In VB.NET, nq doesn't even appear to work because I get Expression expected printed back out at me.
Is there a different way to make newlines actually show as separate lines in the immediate or command window in VB.NET?
An easier way in visual studio 2015 is to do this in the debug window:
?string,nq
You can also do the debug version in earlier versions of VS
?System.Diagnostics.Debug.Print(string,nq)
I've discovered that the solution is to use System.Diagnostics.Debug.Print(). To reproduce the original issue, entering this into the Immediate window...
? "a" & vbCrLf & "a"
...just returns this:
"a a"
However, using this...
System.Diagnostics.Debug.Print("a" & vbCrLf & "a")
...actually shows the newline:
a
a
When clicking in a specific subroutine in Visual Basic 2010, the application crashes giving only the error message "Microsoft Visual Basic 2010 [Express] has encountered a problem and needs to close."
Nothing appears in the log and safe mode doesn't help.
When I had this problem, I found it was due to a line continuation over 400 lines.
For example:
Dim someString As String
someString = "some text and then a line continuation " & _
"and then some more text for 400 lines " & _
... & _
"and then finish the string"
To fix it, I just had to make my code a little sloppier by combining multiple "lines" on one line.
Hope this helps someone else!
It sounds like you are clicking on a module or a .vb file in the solution space. Try right clicking, then "edit code" if at all possible. If there is an Interop object, perhaps a control (.OCX) that not's registered ion Windows, then VS will crash -- promise!