Home > Software engineering >  Format must be the same, and not more than a comma also cannot little a comma
Format must be the same, and not more than a comma also cannot little a comma

Time:11-25

I use the language of vba, I am a beginner, wrote a small program, there is now a difficult!

. Our company want to use the test of the original file (CSV) to modify the value, as shown above 0 to 25, and then to customer testup (CSV), otherwise we are use notepad to open the test file to change to 25, but we work is very busy often have colleagues accidentally mistaken delete the comma, it'll be up shit creek, because we want to back to the customer testup must. (CSV) format with the test of the original file (CSV) exactly a others, so I use excel to open, so you don't mistakenly deleted! Documents and then save the new testup CSV, but, you see testup. (CSV) is much many commas, this is not acceptable, must be original format with the test file (CSV) exactly a others,

So I wrote a program, but they cannot run out? Which could save it for me?



Option Explicit

Dim inFilePath As String

Sub auto_open ()

Dim nRow, nCol As Integer
Dim dataArray arrLine, oneLine
Dim outFilePath As String

InFilePath=selectFile ()



The Open inFilePath For Input As # 1
DataArray=Split (Input $(LOF (1), # 1), vbLf)
Close # 1

For nRow=LBound (dataArray) To UBound (dataArray)
ArrLine=Split (dataArray (nRow), ", ")
For nCol=LBound (arrLine) To UBound (arrLine)
Range (" A1 "). Offset (nRow, nCol). (nCol)
Value=https://bbs.csdn.net/topics/arrLineNext
Next nRow


End Sub
Sub writeTXT ()
Dim nRow, nCol As Integer
Dim dataArray arrLine, oneLine
Dim outFilePath As String
Dim number As a String
Dim the Response As Integer
Dim answer As Boolean

'=============================
'1 of 2 Text file to Excel
'=============================


The Open inFilePath For Input As # 1
DataArray=Split (Input $(LOF (1), # 1), vbLf)
Close # 1

'=============================
'2 of 2 Excel to Text file
'=============================

Do

Number=InputBox (" please enter the work order number ", "archive", "110 @ @ @ @ @")


The cancellation of the 'direct dialog
Then If Len (number)=0
MsgBox (" you cancel the input ")
Workbooks (" cell. XLSM "). The Close SaveChanges:=False

Direct turn off dialog
'ElseIf number=vbNullString Then
MsgBox (" you don't have the input data ")
Workbooks (" cell. XLSM "). The Close SaveChanges:=False

ElseIf Len (number) & lt;> 8 Then

MsgBox (" please enter the eight digital ")


ElseIf Not IsNumeric (number) Then
MsgBox (" please enter the number ")

The Else

The Response=MsgBox (" you input information data is: "& amp; Number, vbYesNoCancel)
If the Response=6 Then
Answer=True
ElseIf Response=2 Then
Workbooks (" cell. XLSM "). The Close SaveChanges:=False
End the If

End the If

Loop Until Len (number)=8 And IsNumeric (number) And answer=True





'Creat new file

OutFilePath="C: \ destination " & amp; Left (inFilePath, InStrRev (inFilePath, "110") - 1) & amp; Number & amp; "CSV"
MsgBox (outFilePath)

The Open outFilePath For Output As # 2

For nRow=LBound (dataArray) To UBound (dataArray)
ArrLine=Split (dataArray (nRow), ", ")

OneLine=arrLine (LBound (arrLine)), 'The first column

For nCol=LBound (arrLine) + 1 To UBound (arrLine)
OneLine=oneLine & amp; ", "& amp; Range (" A1 "). Offset (nRow, nCol). The Value
Next

Print # 2, oneLine
Next nRow

Close # 2
End Sub


The Public Function selectFile () As String

Application. FileDialog (msoFileDialogFilePicker). InitialFileName="C: \ temp "

If Application. FileDialog (msoFileDialogFilePicker.) the Show=1 Then

SelectFile=Application. FileDialog (msoFileDialogFilePicker). SelectedItems (1)

The Else
Workbooks (" cell. XLSM "). The Close SaveChanges:=False

End the If


End the Function









CodePudding user response:

Simple, write a program to keep the Excel good. CSV file handle again,
Remove the comma at the end of each row in the all,

CodePudding user response:

Originally you "original document" also have a comma at the end of ah,

That will keep the original document; The modified save as a new document;
Write a program to the original document, the new document the comparison of one line,
The original document at the end of a line a few commas, after processing of "new document" corresponding lines retain a few commas,
So,

CodePudding user response:

The way a lot of
Placeholder: the configuration file content into the template file, the need to modify the variable substitute placeholder, such as & lt; Para1 & gt; , & lt; Para2 & gt; To generate configuration files, the placeholder symbols replaced with the corresponding numerical,
Number 2. A comma: again a few commas, the original file count over the end of the editing files have a few commas, compare the two number is consistent,
And have decided to use VBA, so this config file can also be directly with code modifications, avoids manually modify the possibility of error,
  •  Tags:  
  • VBA
  • Related