Reading consequent text files by using arrays in VBA - excel

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

Related

Excel VBA: Input box with control characters

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).

VBA Dynamically Building A Formula From An Array

I am trying to dynamically construct a formula based on an array that I have generated from a cell (separated by commas), as there is a varying amount of elements in the array I need to append a new "formula block" with the updated element to use in a if statement that is generated after the for each loop. VBA is throwing a type mismatch error in the InvestigateFormula = line, here is my code:
For Each Type In ToIgnore()
InvestigateFormula = "(ISNUMBER(SEARCH(*" & ToIgnore(Type) & "*," & _
AssetTypesCol & "2)),"
FullFormula = InvestigateFormula & FullFormula
Next Asset
FinalInvestigateFormula = "=IF(OR" & FullFormula & "),""Ignore"", """")"
ActiveCell.Formula = FinalInvestigateFormula
Please let me know if there is an easier way of doing this or how I might be able to correct the above code. Btw I am not declaring a variant I am simply declaring ToIgnore() as String and using the split function from the variable which contains the comma separated values to generate the array/items to loop over.
"Type" is a reserved name? Try strType instead?

Importing Excel and using it for Stata do code

For example, suppose I have this Excel file.
Then, I am manually putting things on Excel into do file like this.
replace A = 1 if B>=1 & B<=6
replace A = 2 if B>=23 & B<=2
replace A = 3 if B>=3 & B<=1
replace A = 4 if B>=5 & B<=3
If this wasn't clear, please see this image to see what I am doing.
But there could be actually hundreds of lines.
How can write a short code which imports the Excel file, and another short code which replaces the manual codes I have written?
So the goal here is just to make my code succinct.
You can import excel this file. Let's suppose the headers are A and B and the import produces those as numeric variables. Then the text of a new do-file is contained within
gen text = "replace A = " + string(_n) + " if inrange(A, " + string(A) + "," + string(B) + ")"
which you must export and then run on your real data.
Not tested. I'd also suggest considering doing this in your favourite text editor.
Note that many of your comparisons in your example will always be false.

How to convert part of a cell value to bold

I have the below VBA code and A and B are holding some strings. I want to concatenate these values with some other strings and store the result in a different cell, but I want only the strings in A and B to be formatted as bold and the rest as normal text.
Set A = Worksheets("Mapping").Cells(rowNumber, columnNumber)
Set B = Worksheets("Mapping").Cells(rowNumber, 3)
' E.g.: A="currency", B="Small Int"
Worksheets("TestCases").Cells(i, 2) = "Verify the column " & A & " has same Data type " & B & " in code as well as Requirement document"
Expected output:
Verify the column currency has same Data type Small Int in code as well as Requirement document
Note: The values of A and B keep changing, so we cannot use the Characters() function.
Any help will be highly appreciated.
You can use the Characters() method - you just need to keep track of the length of the substrings. Personally, I would store the static strings in variables so that I can change them later without having to recalculate the indexes by hand:
' Untested
Set A = Worksheets("Mapping").Cells(rowNumber, columnNumber)
Set B = Worksheets("Mapping").Cells(rowNumber, 3)
Dim S1 = "Verify the column "
Dim S2 = " has same Data type "
Dim S3 = " in code as well as Requirement document"
With Worksheets("TestCases").Cells(i, 2)
.Value = S1 & A & S2 & B & S3
.Characters(Len(S1), Len(A)).Font.Bold
.Characters(Len(S1)+Len(A)+Len(S2), Len(B)).Font.Bold
End With
The function to change the font style is:
[Cells/Cell range].Font.FontStyle = "Bold"
Therefore something like might work:
Worksheets("Mapping").Cells(rowNumber, columnNumber).Font.FontStyle = "Bold"
You can also make things have underlines, strikethroughs etc... I found this really helpful blog post which goes through everything you should need to know:
http://software-solutions-online.com/excel-vba-formating-cells-and-ranges/#Jump4
I think you should have searched for this information yourself... Nevertheless this is the code that you should use to convert some cell data to bold:
Worksheets("Mapping").Cells(rowNumber, columnNumber).Font.Bold = True

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

Resources