Write comma-delimited text file in vba, elegantly - excel

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.

Related

Carriage return and line feed not being replaced into VBA string

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), " ")

Reading consequent text files by using arrays in VBA

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

How to substitute multiple line feed with a single line feed

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

Separate words with commas in Excel 2010

I'm trying to use a formula in Excel to separate a bunch of words in a cell with a comma. If there are more than 5 words in the cell, I just want to get the first 5 words. To get the first five words in a cell and separate them by a comma I use this:
=SUBSTITUTE(LEFT(A1,FIND("^",SUBSTITUTE(A1," ","^",5))-1), " ", ", ")
This works fine. But the problem with this, because of the number 5 here, if I a cell contains less than 5 words, I get an error. I tried to substitute the 5 with this:
LEN(TRIM(A1))-LEN(SUBSTITUTE(A1," ",""))+1
So my function becomes this:
=SUBSTITUTE(LEFT(A1,FIND("^",SUBSTITUTE(A1," ","^",LEN(TRIM(A1))-LEN(SUBSTITUTE(A1," ",""))+1))-1), " ", ", ")
But this doesn't work, it gives me an error. Any idea how I can do this please?
Also I would like to ignore the first word if its first character is "-" (without the quotes) and just start from the second word. So in other words, I want something like this:
I love my life very much should return I, love, my, life, very
- I love my life very much should return I, love, my, life, very (the "-" is ignored")
I love my should return I, love, my
Thanks in advance for any help
Here's a somewhat different approach. Aside from the "less than 5" issue, it also deals with the "5 words with no space at the end" issue:
=LEFT(A1,FIND("^",SUBSTITUTE(A1 & "^"," ","^",5))-1)
EDIT 1: I just noticed the part about the leading "- ". My addition isn't very elegant, but it deals with it, and also TRIMS any trailing spaces:
=TRIM(LEFT(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1),FIND("^",SUBSTITUTE(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1) & "^"," ","^",5))-1))
EDIT 2: Oh yeah, commas:
=SUBSTITUTE(TRIM(LEFT(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1),FIND("^",SUBSTITUTE(IF(LEFT(A1,2)="- ",MID(A1,3,999),A1) & "^"," ","^",5))-1))," ",",")
Try this:
=TRIM(LEFT(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"-"," "))," ",","),",",REPT(" ",99),5),99))
This will work even if there is not a space after the dash or if there are extra spaces in the text. Often I find that input is not very clean.
=SUBSTITUTE(LEFT(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"-","",1)),
" ","*",5),IFERROR(FIND("*",SUBSTITUTE(TRIM(SUBSTITUTE(A1,"-","",1)),
" ","*",5))-1,999))," ",",")
Edit: After commenting on István's, I made mine flawless too.
=SUBSTITUTE(LEFT(SUBSTITUTE(TRIM(SUBSTITUTE(LEFT(TRIM(A1),1),"-"," ",1)
&MID(TRIM(A1),2,999))," ","*",5),IFERROR(FIND("*",SUBSTITUTE(
TRIM(SUBSTITUTE(LEFT(TRIM(A1),1),"-","",1)&MID(TRIM(A1),2,999))," ","*",5))-1,999))," ",",")
But I think his is more elegant.
Try this:
=SUBSTITUTE(LEFT(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "),", ","|",MIN(LEN(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "))-LEN(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", ")," ","")),5)),FIND("|",SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "),", ","|",MIN(LEN(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", "))-LEN(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"- ","",1))&" "," ",", ")," ","")),5)))-1),",,",",")
The formula works by taking the following steps:
Remove any leading dash-space
Trim any leading or trailing spaces
Insert comma-spaces in place of spaces and add a trailing comma-space
Calculate the lesser of 5 and the number of words in the string
Put in "|" in place of either the fifth comma-space or the trailing comma-space if the string is less than five words
Determine the position of the "|"
Strip off the "|" and all characters to the right of it
Remove any doubled commas due to any single embedded commas in the initial string
If you are willing to consider a VBA solution, this complex expression can be replaced by a user-defined function:
Function words5(InputString As String) As String
Dim wordArray As Variant
wordArray = Split(Trim(Replace(InputString, _ 'remove "-", put words into array
"-", "", , 1)), " ")
ReDim Preserve wordArray(LBound(wordArray) To _ 'drop all but the first 5 words
WorksheetFunction.Min(UBound(wordArray), 5 - 1))
words5 = Replace(Join(wordArray, ", "), ",,", ",") 'rejoin the words with ", "
End Function 'separator
On the plus side of using this code is its maintainability compared to the worksheet formula, which impossible to understand or safely alter without access to the original building blocks that were combined into the single expression.
The code would have to be installed in the workbook in which it is used or in either the standard Personal.xlsb workbook or an addin workbook.
To use the function, copy and paste it into a standard module, which can be inserted into a workbook via the VBA editor. You can open the editor with the Visual Basic button on the `Developer tab of the ribbon.
Figured I'd throw my hat in the ring also. I think this formula should cover the bases:
=SUBSTITUTE(TRIM(LEFT(SUBSTITUTE(TRIM(SUBSTITUTE(A1&" ","- ",""))," ",REPT(" ",99)),99*5))," ",",")

VBS - Ignoring the \ special character properties

I'm struggling to call a command line correctly in VBS due to the \ escape character.
The string output I'm looking to write to the command line is,
batch_name=\"myBatch\"
Which gets passed to a .exe file. Unfortunately, due to the way the \ character works I can only write,
batch_name=\myBatch\
batch_name=\""myBatch\""
I can't get \" in the output! An altered version of my code is below,
BATCH_NAME = "myBatch"
outputString = "batch_name=\" & BATCH_NAME & "\"
I've tried lots of methods - Concatenating the string with Chr(34), using multiple double quotes, even trying to replace() "" with ", nothing seems to work.
Any ideas?
I gave it a shot and
outputString = "batch_name=\""" & BATCH_NAME & "\"""
worked for me giving the result batch_name=\"myBatch\"
Does it work for you? How you execute this command in the shell?
You can do the following;
outputString = "batch_name=" & chr(92) & chr(34) & BATCH_NAME & chr(92) & chr(34)

Resources