vba的 读取文件 Sub test() Open "D:\temp\文件名.txt" For Output As #1 Close #1 End Sub 读取 Sub test() Open "D:\temp\文件名.txt" For Input As #1 While Not EOF(1) Line Input #1, s If Trim(s) <> "" Then Selection.TypeText Text:=s + vbCrLf End If Wend Close #1 End Sub
Open "D:\temp\文件名.txt" For Output As #1 print #1,内容 Close #1
以下代码是我程序中一个导出仿真结果的sub,其中文件名是由当前用户名+当前日期+当前时间+3位随机数字组成。Sub ExportRange() Dim NumRows As Long, NumCols As Integer Dim r As Long, c As Integer Dim Data Dim OutputRng As Range Dim InputRng As Range
Dim Filt As String Dim FilterIndex As Integer Dim myFileName As Variant 'Dim DefaultPath As String Dim InitFileName As String Dim Title As String Dim i As Integer Dim Msg As String
Dim CreateTiem As String, curAuthor As String, ModifiedInfomation As String Dim Fname As String, suDictionary As String, myPath As String
Set InputRng = Sheets("输入").Range(Sheets("输入").Cells(1, 1), Sheets("输入").Cells(ROWSCOUNT + 1, 39)) Set OutputRng = Sheets("输出").Range(Sheets("输出").Cells(1, 1), Sheets("输出").Cells(ROWSCOUNT + 1, 25))
Close #1
读取文件
Sub test()
Open "D:\temp\文件名.txt" For Output As #1
Close #1 End Sub
读取
Sub test()
Open "D:\temp\文件名.txt" For Input As #1
While Not EOF(1)
Line Input #1, s
If Trim(s) <> "" Then
Selection.TypeText Text:=s + vbCrLf
End If
Wend
Close #1
End Sub
print #1,内容
Close #1
Dim NumRows As Long, NumCols As Integer
Dim r As Long, c As Integer
Dim Data
Dim OutputRng As Range
Dim InputRng As Range
Dim Filt As String
Dim FilterIndex As Integer
Dim myFileName As Variant
'Dim DefaultPath As String
Dim InitFileName As String
Dim Title As String
Dim i As Integer
Dim Msg As String
Dim CreateTiem As String, curAuthor As String, ModifiedInfomation As String
Dim Fname As String, suDictionary As String, myPath As String
Set InputRng = Sheets("输入").Range(Sheets("输入").Cells(1, 1), Sheets("输入").Cells(ROWSCOUNT + 1, 39))
Set OutputRng = Sheets("输出").Range(Sheets("输出").Cells(1, 1), Sheets("输出").Cells(ROWSCOUNT + 1, 25))
myDate = Format(Date, "yyyymmdd")
myTime = Format(Time, "hhmmss")
Randomize
myRnd = Format(100 * Rnd + 1, "00#")
curUserName = Worksheets("用户数据").Range("IK501").Value
timeS = myDate & myTime & myRnd
Fname = curUserName & timeS & ".csv"
subDirectory = "\仿真结果\"
myPath = ThisWorkbook.Path & subDirectory
If PathExists(myPath) = False Then MkDir (myPath)
myFileName = myPath & Fname
Open myFileName For Output As #1
NumCols = InputRng.Columns.Count
NumRows = InputRng.Rows.Count
For r = 1 To NumRows
For c = 1 To NumCols
Data = InputRng.Cells(r, c).Value
If IsNumeric(Data) Then Data = Val(Data)
If IsEmpty(InputRng.Cells(r, c)) Then Data = ""
If c <> NumCols Then
Write #1, Data;
Else
Write #1, Data
End If
Next c
Next r
NumCols = OutputRng.Columns.Count
NumRows = OutputRng.Rows.Count
For r = 1 To NumRows
For c = 1 To NumCols
Data = OutputRng.Cells(r, c).Value
If IsNumeric(Data) Then Data = Val(Data)
If IsEmpty(OutputRng.Cells(r, c)) Then Data = ""
If c <> NumCols Then
Write #1, Data;
Else
Write #1, Data
End If
Next c
Next r
'写入本次仿真的信息 包括生成时间,作者,修改变量
CreateTime = Now
curAuthor = curUserName
ModifiedInfomation = Worksheets("用户数据").Range("II501")
Write #1, "生成时间";
Write #1, CreateTime;
Write #1, "用户";
Write #1, curAuthor;
Write #1, "修改的变量";
Write #1, ModifiedInfomation;
Close #1MsgBox "导出仿真结果 " & myFileName & " 成功!", vbInformation, PROGRAMTITLEEnd Sub
myTime = Format(Time, "hhmmss")
Randomize
myRnd = Format(100 * Rnd + 1, "00#")
timeS = myDate & myTime & myRnd
Fname = curUserName & timeS & ".csv"
subDirectory = "\仿真结果\"
myPath = ThisWorkbook.Path & subDirectory
If PathExists(myPath) = False Then MkDir (myPath)
myFileName = myPath & Fname