I have created a code where I automate text files, outputting column data from an excel macro, in a specific format. I have created 5 different sub methods where they all contain almost the same lines of code. However, there are two lines of code that change for each sub. I would like to create just ONE sub just to simplify the coding for the user. The end goal is to have just one function that can be called and automatically generate the other output files (from sub test1, sub test2, sub test3, sub test4).
Below is one of the sub function code. The rest are the same except for the following lines:
stream.Write "EQUIPMENT_ID_DEF,02,0x1" & "," & Chr(34) & "ic1080_1" & Chr(34)
For the above line what changes is the 0x1 (it increases) and the "ic1080_1" which changes its name to test1, test2, etc...
If destgroup = "ic1080_1" And ssystem = "A429" And sformat = "BNR" Then
For the above line what changes is the "ic1080_1" name for the other sub names (test1, test2, etc...)
Sub ic1080_1(Path, IDnum As Integer, parmgroup As String)
'Declaring variables
Dim equipID As String, destgroup As String, sourceparmname As String, descript As String
Dim lsb As Integer, msb As Integer, signed As String, sformat As String, units As String
Dim scalefact As Variant, numbits As Integer, decim As Integer
Dim ssystem As String
Dim FName As String, stream As TextStream
Dim fso As Scripting.FileSystemObject
Dim vDB
Set fso = New Scripting.FileSystemObject
'Create txt file
Set stream = fso.CreateTextFile(Path)
'Activate Sheet1
Sheet1.Activate
With Sheet1
vDB = .Range("a1").CurrentRegion 'Get data to array from excel data range
n = UBound(vDB, 1) 'Size of array (row of 2 dimension array)
End With
'Open text file to write data
stream.Write "EQUIPMENT_ID_DEF,02,0x" & IDnum & "," & Chr(34) & parmgroup & Chr(34)
'Create arrays for each row of data
For i = 2 To n
destgroup = vDB(i, 15) '15th columm array(destination group)
ssystem = vDB(i, 7) '7th columm array(source system)
sformat = vDB(i, 32) '32nd columm array(format)
sourceres = vDB(i, 11) '11th column array(source resolution)
If destgroup = parmgroup And ssystem = "A429" And sformat = "BNR" Then
sourceparmname = format(Val(Replace(vDB(i, 8), "label ", "")), "0000")
descript = vDB(i, 3)
signed = Val(Replace(vDB(i, 33), "Yes", 1))
msb = vDB(i, 34)
lsb = vDB(i, 35)
units = vDB(i, 6)
numbits = (msb - lsb + 1) 'Calculates the number of bits
scalefact = sourceres * (2 ^ (numbits)) 'Computes the scale factor by: source resolution *(2^(msb-lsb+1))
decim = 9
'Write data into text file
stream.Write vbCrLf & "; #### LABEL DEFINITION ####" & vbCrLf & _
"EQ_LABEL_DEF,02," & sourceparmname & vbCrLf & _
"UDB_LABEL," & Chr(34) & descript & Chr(34) & vbCrLf & _
"STD_SUB_LABEL," & Chr(34) & descript & Chr(34) & "," & lsb & "," & msb & "," & signed & vbCrLf & _
"STD_ENCODING," & Chr(34) & sformat & Chr(34) & "," & Chr(34) & units & Chr(34) & "," & scalefact & "," & numbits & "," & decim & vbCrLf & _
"END_EQ_LABEL_DEF"
End If
'Continue looping until the last row
Next i
stream.Write vbCrLf & "; #### END EQUIPMENT ID DEFINITION ####" & vbCrLf & _
"END_EQUIPMENT_ID_DEF"
'Close the text file
stream.Close
End Sub
I also created another sub that calls all the subs ("ic1080_1", test1, test2, test3, test4) to output all the text files and saves them into a folder:
Sub txt_files()
Dim fso As Scripting.FileSystemObject, NewFolderPath As String
Dim Path As String
'Retrieve Target Folder Path From User
NewFolderPath = Application.GetSaveAsFilename("")
Set fso = New Scripting.FileSystemObject
If Not fso.FolderExists(NewFolderPath) Then
fso.CreateFolder NewFolderPath
End If
'Call sub functions to generate text files and store them in NewFolderPath
Call ic1080_1.ic1080_1(NewFolderPath & "\ic1080_1.txt", 3, "ic1080_1")
Call ic1080_1.ic1080_1(NewFolderPath & "\test1.txt", 4, "test1")
End Sub
"ic1080_1"and"ic1080_2"name for the other sub names (test1,test2, etc...)?Sub "ic1080_1"(Path)isn't valid VBA. What's going on there?Left(destgroup, 7) = "ic1080_1"will always be false, because no seven character string will be equal to the string"ic1080_1"(which has 8 characters(