VBA: Debug.Print without newline? - vba

For debugging in VBA, I have several Debug.Print statements throughout my code. For a line-parsing block, I'd like to print the line, and output any flags inline with the line, without having multiple Debug.Print sFlag & sLine statements throughout the many if/elseif/else blocks.
Is there a way, within VBA, to suppress the newline at the end of a Debug.Print statement?

It turns out you can easily do this by simply adding a semicolon to the end of your Debug.Print statement. Like so:
While oExec.StdOut.AtEndOfStream = False
sLine = oExec.StdOut.ReadLine
If bInFileSystem Then
If AnalyzeFileSystemLine(sLine) = False Then
Debug.Print "[FSERR]: ";
End If
ElseIf bInWASCheck Then
If AnalyzeWASLine(sLine) = False Then
Debug.Print "[WASERR]: ";
End If
End If
Debug.Print sLine
Wend
So, some sample output will be:
test text things look good!
[FSERR]: uh oh this file system is at 90% capacity!
some more good looking text :)
[WASERR]: oh no an app server is down!

Related

Infinite loop on the currency symbol alt key code 0164

In my sript I use
If Selection.Text Like "* [aAwWzZiIoOuUVQ] *"
but if script check a table falls into infinite loop on the currency symbol alt key code 0164.
I use MsgBox Selection.Text and loops on such a line (and similar with marks 0164):
MsgBox show:
How to skip checking such a line in the table, go further in the code or exit the For Next loop.
Something like:
If Selection.Text = ChrW(164) Then
[EDIT]
Ok my fault.
This is 7 lines not 1.
And selected text is not ChrW(164) but NOTHING.
I try If Selection.Text = "" Then
or
If Selection.Text = ChrW(0) Then
but loop not stop.
[EDIT2]
Dim NumLines As Long
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
NumLines = Selection.Range.ComputeStatistics(wdStatisticLines)
MsgBox "Lines to check " & (NumLines) & " If you want STOP: Press CTRL+Pause Break"
Selection.Collaps
For i = 1 To NumLines...
Now I understand where the mistake is.
The macro did not loop indefinitely, even though it seemed to. It didn't hang on any particular character, it simply counted each table cell as a separate line and took that value as the final digit of the loop counter.

If text compairson statement failing in Word VBA

I've been fighting with this for a while and I'm not getting why the if statement is returning true when one of the AND statements is obviously false.
strtmp = TypeName(ctl) 'Take for instance (and have verified) this returns String type with the value of Label. However the below is returning true in this case.
If (strtmp = "TextBox") And (arControlName(2) = "Desc") And Trim(Me.Controls(ctl.Name).Value & "") = "" Then
'Do some stuff
End If
My problem is the first statement of the if is somehow equating to true OR "And" is not working as I expect "The AND = TRUE if all conditions are TRUE. The AND=FALSE if any of the conditions are FALSE." OR I'm using it wrong?
Perhaps I am simply loosing my marbles as well :)
EDIT: If the statements are broken out into 3 separate If statements (like below), the code works as expected.
If (strtmp = "TextBox") Then
If (arControlName(2) = "Desc") Then
If Trim(Me.Controls(ctl.Name).Value & "") = "" Then
' Set the value and exit loop
End If
End If
End If
Thanks to the help of the comments that got my brain jogging in a different direction, I've determined the problem.
It apparently has something to do with VBA and not liking to execute certain calls in an If Statement It seems to work fine putting the TypeName(ctl) = "TextBox" in the if statement but not the call to Me.Controls(ctl.Name).Value & "".
strctlTypeName = TypeName(ctl)
strctlVal = Trim(Me.Controls(ctl.Name).Value & "")
If ((strctlTypeName = "TextBox") And (arControlName(2) = "Desc")) And strctlVal = "" Then 'And (Trim(Me.Controls(ctl.Name).Value & "") = "") Then
'Set the value and exit inner for loop
End If

Replace string and rest of line with other string

So here's what I want to do:
I have a string origStr, which is at the beginning of a line in a Word document. After origStr is an unknown value (e.g. 23 or 2,6)
I want to find that string and replace the value after with another known value, but keep the string.
Example in my Word document:
Diam. diastole, mm: 53
[Running VBA makro where user input is 54,3]
Expected Result:
Diam. diastole, mm: 54,3
Actual result:
Diam. diastole, mm: 54,3 53
So here's what i got:
origStr = LArray(i, j - 1).Caption & ": *"
replStr = LArray(i, j - 1).Caption & ": " & TBArray(i, j).Text
With ActiveDocument.Content.Find
.MatchWildcards = True
.ClearFormatting
.Text = origStr
.Replacement.ClearFormatting
.Replacement.Text = replStr
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindStop
End With
Why isn't my old value being deleted?
Thanks in advance!
Word uses "lazy" pattern matching which is good in this case because otherwise it would just replace the rest of your entire document. Vincent G gave a solution for one word (no more than one space in the rest of the line), here is something more general:
To get Word to replace everything until the end of the line, you need to include the end of the line into the search string. ^l or ^11 is the control code for new line (shift+enter), ^12 is page or section break, ^13 is carriage return.
This is the most "recent" list I found.
So "yourtext*^11" would match everything from "yourtext" until the next new line and so on.
Depending on your formatting, it could be either one of the above so you have to be careful. If you replace "*^12" but your lines end with new line, you will replace the whole paragraph.
To catch all of them, you can group them: yourtext*[^11^12^13] will match everything until the next new line or carriage return. However if we replace that, the end of the line will be replaced as well so we have to include it in the replacement text. How do we know what it was?
We can use () to define a submatch and \1, \2 etc to use these submatches in the replacement text:
origStr = LArray(i, j - 1).Caption & ": *([^11^12^13])"
replStr = LArray(i, j - 1).Caption & ": " & TBArray(i, j).Text & "\1"
Example:
before:
after replacing with origStr = "yourtext*([^11^12^13])" and replStr = "yourtext newtext\1":
Note: These expressions also work with the Find and Replace dialog, just make sure "Use Wildcards" is enabled
Its not really a VBA problem, the same happening in "Find and Replace" dialog.
Replace your original string with:
origStr = LArray(i, j - 1).Caption & ": <*>"

msgbox dilemma with translator app

I am building language translator for my tribe. I have about 90% it completed. the only problem i am having is that i want to have msgbox pop up and say "translation not found.", when someone enters a phrase or a word that can't be found in the text file which is read " cat | nish stu yah." I can have it pop up when the translation isn't found, but here is the problem when i type in a word that is found the msgbox pop up as well. I dont know if it has to do with that it is running a do while (true) loop or i am just not coding the translator button correctly. the code is:
Do While (True)
Dim line As String = reader.ReadLine
If line Is Nothing Then
Exit Do
End If
Dim words As String() = line.Split("|")
Dim word As String
For Each word In words
If word = TextBox1.Text Then
TextBox2.Text = words(+1)
Else
MessageBox.Show("Translation is not available")
End If
If "" = TextBox1.Text Then
MessageBox.Show("No entry was made.")
End If
Next
Loop
Assuming your file format is:
cat | nish stu yah
with the spaces around the | character, your issue is probably that String.Split will not remove the spaces, so in this case you will end up with:
words(0) = "cat "
words(1) = " nish stu yah"
So, if TextBox1.Text is "cat", it will not match. You could change the comparison to something like this, to make it more forgiving:
If word.Trim().ToLower() = TextBox1.Text.Trim().ToLower() Then
However, you don't really need to loop through words, since you only want to compare the user input against the untranslated version. You are also showing the message box for each line in the translation file. I think you probably want something like this (completely untested):
If "" = TextBox1.Text Then
MessageBox.Show("No entry was made.")
Exit Sub
End If
Dim found As Boolean = False
Do While (True)
Dim line As String = reader.ReadLine()
If line Is Nothing Then
Exit Do
End If
Dim words As String() = line.Split("|"c)
If words(0).Trim().ToLower() = TextBox1.Text.Trim().ToLower() Then
TextBox2.Text = words(1)
found = True
Exit Do
End If
Loop
If Not found Then
MessageBox.Show("Translation is not available")
End If
Still not the most elegant code, but hopefully points you in the right direction.

Replace Underscores characters with Space inside a String value

This is a rather simple question.
I have a date variables formated like: 25_December_2010
I would like a statement or a bit of code in the VBA macro that will transform that string value from: 25_December_2010 to 25 December 2010.
Somehow be able to remove the underscores from inside the String value....
As I mentioned in comments, use code below:
Dim strDate As String
strDate = "25_December_2010"
strDate = Replace(strDate,"_"," ")
I wanted something similar in a macro I'm using for data cleaning so I took #simoco's answer and created a simple but mostly safe macro/sub.
Sub ConvertSpaceToUnderscore()
Dim strCellValue As String
' Use basic error handling if more than 1 cell is selected, or
' possibly if something that isn't a cell is selected.
On Error GoTo SelectionTooBig
strCellValue = Selection.Value
strCellValue = Replace(strCellValue, " ", "_")
Selection.Value = strCellValue
On Error GoTo 0
' Exit the sub if things went well
Exit Sub
SelectionTooBig:
MsgBox "Please select one cell at a time.", vbCritical, "Selection too large"
End Sub