I have this data in a single cell wrapped.
This
is
a
bad dream
For my report formatting purpose, i need to reduce multiple line feeds. Whenever there are more than 2 line feeds between 2 strings, they need to be replaced by exactly 2 line feed. So the data must look like
This
is
a
bad dream
I'm just a beginner in excel scripting. I tried using the substitute function but couldn't get the right result.
Can some looping be done using substitute?
Whenever there are more than 2 line feeds between 2 strings, they need to be replaced by exactly 2 line feed.
Use this small function:
Function customSubstitute(myStr As String) As String
customSubstitute = myStr
Do While InStr(1, customSubstitute, Chr(10) & Chr(10) & Chr(10))
customSubstitute = Replace(customSubstitute, Chr(10) & Chr(10) & Chr(10), Chr(10) & Chr(10))
Loop
End Function
Related
I'm using an input box to retreieve QR-codes from reader.
Unfortunately, the parameters in the QR-code are separated by group separator characters (decimal ASCII code 29) and these characters are being omitted.
The read in string contains all the data, but I can't distinguish the single parameters anymore.
What can I do? Is there another way to read in a string WITH all the control characters?
Thank you for your help!
Without further action your inputbox result indeed gets displayed as string without (visible) Chr(29) group separators ... even though the InputBox string result still contains those characters.
Therefore you need to convert the input eventually; the following Example demonstrates possible ways:
Sub testInput()
'a) Provide for QR results
Dim qr(0 To 2) As Variant
'b) Provide for default using Chr(29) group delimiters
Dim DefaultInput
DefaultInput = "a" & Chr(29) & "b" & Chr(29) & "c" & vbNewLine
'c) Execute InputBox (and assign to first QR result)
qr(0) = "0. Visible Input: " & InputBox("Enter QR", "QR input", DefaultInput)
'd) Convert visible inputs to 2nd and 3rd QR result
qr(1) = "1. Replaced Chr(29): " & Replace(qr(0), Chr(29), ",")
qr(2) = "2. Splitted Chr(29): " & Join(Split(qr(0), Chr(29)), "|")
'e) Show all three QR results
MsgBox Join(qr, vbNewLine)
End Sub
Further hint
If you need to get the different group results separately, I'd choose the split function (without joining the 0-based 1-dimensional array elements immediately, which was only done for display in the messagebox).
I'm writing some logs entries into a csv file however when I'm trying to replace CR and LF into a specific string I'm getting the same result as before.
Replace(SQLStatement, vbTab & Chr$(13) & Chr$(10), "")
Not being able to remove CR and LF, and even Tabs, is affecting my csv and finally in the Excel file the string is outputed on more lines instead of just one.
That will only replace the specific combination of vbTab & Chr$(13) & Chr$(10).
If you want to replace them you will need to do the individually.
Replace(Replace(Replace(SQLStatement, vbTab,""), Chr$(13),""), Chr$(10), "")
Although you need to be careful that your lines do not run into one another. You might be safer with
Replace(Replace(Replace(SQLStatement, vbTab," "), Chr$(13)," "), Chr$(10), " ")
I have a macro that prompts me for a SQL query (unless it was called by another Sub, in which case it uses the argument that was passed into its optional string parameter as the query) and then executes the query against my Teradata SQL database.
It works fine, unless there's a column alias containing a space in the query.
Example query:
SELECT 2 + 2 AS "Query Result";
Error:
Run-time error '1004':
[Expression.Error] The name 'Source' wasn't recognized. Make sure it's spelled correctly.
The line of code which I believe is the culprit is as follows (my apologies for the readability-- I recorded the macro, modified it just enough to get it to work somewhat dynamically and then haven't touched it since).
ActiveWorkbook.Queries.Add Name:=queryName, formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Odbc.Query(""dsn=my-server-name"", " & Chr(34) & code & Chr(34) & ")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
I assume it has to do with the fact that the example query above has double quotes for the alias, which are confusing the syntax when trying to be interpolated. Any help would be greatly appreciated!
Here's what the string for the formula is set to in this line:
ActiveWorkbook.Queries.Add Name:=queryName, formula:=<string here>
after all the chr() and concatenation are done:
let
Source = Odbc.Query("dsn=my-server-name", "<code>")
in
Source
That token <code> is replaced by whatever is in your variable code. So I suspect you are correct in that this formula would need to have it's double quotes escaped fully.
In other words this string you are building form Formula is going to be evaluated as code itself, and even in that evaluation it will be passing more code (your SQL) onto the Teradata server to be evaluated there.
You are in code inception. VBA code writing powerquery code writing Teradata code.
Understanding that and guessing a bit here, I'm thinking your current code variable looks something like:
code="SELECT 2 + 2 AS ""Query Result"";"
Your double quotes are already escaped for VBA. BUT because you have to survive another round of eval in powerquery you need to escape once again. Instead:
code="SELECT 2 + 2 AS """"Query Result"""";"
*I think...
I am trying to write a VBA code to read values and write it to where I want from 4 thousand different text files.
As an example the fine name is like NACA63220_1.30_17_CD.txt and NACA63220_1.05_12_CL.txt
In this name, the value 1.30 changes, 17 changes and CD becomes CL etc.
I want to create loops so that I read and paste the value I want from these files one by one.
Mach = Array ("0.2_", "0.6_", "0.9_", "1.05_", "1.30_")
Alpha = Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
Letter = Array("_CD", "_CL", "_CM")
strFile = D:\Database\NACA63220_ + Mach(5) + Alpha(18) + Letter(1) .txt
I want to have something like this with loops so that in this instance this strFile becomes D:\Database\NACA63220_ 1.30_17_CD.txt and then I can continue with my code.
You need to concatenate strings with & not + (which is for calculation only). Also your strings need to be enclosed in quotes "".
strFile = "D:\Database\NACA63220_" & Mach(5) & Alpha(18) & Letter(1) & ".txt"
Note that depending on how your arrays were defined the counting starts with zero 0 not with 1. So the last item is Mach(4) not Mach(5). In this case …
strFile = "D:\Database\NACA63220_" & Mach(4) & Alpha(17) & Letter(0) & ".txt"
should give the desired result D:\Database\NACA63220_ 1.30_17_CD.txt
I just have to ask this for once...
Is there a more elegant way of having your output comma-delimited, than the tried and tested & "," & we always use?
print #myTextFileNumber, myValue1 & "," & myText2 & "," & myValue3
the command you are looking for is write
from the help:
the Write # statement inserts commas between items and quotation marks around strings as they are written to the file
in your example, then command would be:
write #myTextFileNumber, myValue1 , myText2 , myValue3
If you store your values in an array, you can use join.