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
Related
I am trying to read from the clipboard and place in an array. I want to validate the first line as the text: "Client Code "
I fill the array with:
tbClipBoardContents.Text = My.Computer.Clipboard.GetText()
For Each strLine As String In tbClipBoardContents.Lines
arrClipBoard.Add(strLine.ToString)
Next
When I give a variable the value of the first array entry it appears correct = "Client Code " in Visual Studio debugging.
Dim test As String = Trim(arrClipBoard(0).ToString)
However when I check using the "IF" statement it tells me it is not correct??
If test = "Client Code " Then
MsgBox("Correct Clipboard Structure")
Else
MsgBox("Not a Valid Clipboard Structure: " & Trim(arrClipBoard(0).ToString)) ' ** Fires this response.
End If
What is doing my head in is that if I copy the value of test from VS debugger and paste it in the if statement it looks like "Client Code " but this time the if statement fires the correct response.
I have tried it by filling the textbox (tbClipBoardContents) using:
tbClipBoardContents.text.split(New [Char]() {CChar(vbCrLf)})
and
tbClipBoardContents.text.split(newvbline)
with the same results.
So does this mean the true value from the clipboard for the line "Client Code " also carries some hidden characters? Any help is appreciated.
Brad
P.S. I have found that if I test the value of Mid(test,1,11) then I will get the desired result, so this is a workaround but would be interested to know what the 12th character is? Perhaps it is the "CChar(vbCrLf)"
As per my observation Trim will have removed the spaces. and you should change the condition to "ClientCode".
The final solution that worked for me was to find the trailing space using Andrew Morton's method above then
arrClipBoard.Add(Trim(Regex.Replace(strLine.ToString, Convert.ToChar(160), "")))
which effectively converted the char(160) to "".
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")
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'd like to add code to my template where the macro asks "What page is the table on?" and the user can type in the response, i.e., 38. Afterward, the text, "[Table Page 38]" is inserted, one after the other, into the same, specified Word document.
Dim strPageInfo As String
strPageInfo = InputBox("Type page number for table placeholder.")
Any help would be greatly appreciated.
The following snippet will just insert the input number into the text selection. I am not sure if this is exactly what you are looking for.
Dim strPageInfo As String
strPageInfo = InputBox("Type page number for table placeholder.")
Selection.TypeText Text:="[Table Page " + strPageInfo + "]";
Here's a list of macros for your reference as well. You may be able to find exactly what you are needing to do if this doesn't work.
http://wordribbon.tips.net/C0694_Macros.html