VBA学习笔记(2)之文件操作
来源:互联网 发布:淘宝便宜的衣服能买吗 编辑:程序博客网 时间:2024/06/14 17:49
今天学到了有关文件的创建以及读写的一些操作
Dim MyFile,MyName,MyPath
1.文件另存为
2.查找指定目录的文件名
MyName = Dir(MyPath,vbDirectory) '指定的是在MyPath这个路径下的文件名,从第一个文件开始,这里我们可以做一个循环,循环体内不要忘记写上MyName=Dir,表示寻找下一个文件了
3.查找指定目录下指定后缀的文件名
MyFile= Dir("C:\*.TXT")此为寻找该目录下所有的txt文件,同样用loop来实现,MyFile= Dir("C:\*.TXT",vbHidden)表示隐藏的.txt文件,MyFile=Dir寻找下一个文件。*可以指定成具体的文件名,则无需循环
4.在指定目录下的文件里写入数据
Open MyPath & MyFile For Append AS #1
Print #1,"这是追加的内容"
Close #1
代码事例:
'??
Private Sub Button_Submit_Click()
Dim shp As Shape
Sheets("sheet1").Copy
For Each shp In ActiveSheet.Shapes
shp.Delete
Next
'save as a file
ActiveWorkbook.SaveAs Filename:=Range("B13"), FileFormat:=xlExcel8
ActiveWorkbook.Close
MsgBox "success"
'read files
Dim MyName, MyFile, MyPath
Dim i&
i = 1
MyPath = Sheets("sheet1").[B11] & "\"
MyName = Dir(MyPath, vbDirectory)
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
'If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
'ActiveSheet.Range("A1") = MyName
Open Range("B13") For Append As #1
Print #1, Cells(1, i).Value & MyName
i = i + 1
Close #1
' End If
End If
MyName = Dir 'search next directory
Loop
目前上述代码有问题,那个Cells(1, i).Value&; MyName是没有成功的,是过了Cells(1, i).Value = MyName,无论如何都没有成功,其他的都是对的.
Private Sub Button_Submit_Click()
Dim shp As Shape
Sheets("sheet1").Copy
For Each shp In ActiveSheet.Shapes
shp.Delete
Next
'save as a file
ActiveWorkbook.SaveAs Filename:=Range("B13"), FileFormat:=xlExcel8
ActiveWorkbook.Close
MsgBox "success"
'read files
Dim MyName, MyFile, MyPath
Dim i&
i = 1
MyPath = Sheets("sheet1").[B11] & "\"
MyName = Dir(MyPath, vbDirectory)
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
'If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
'ActiveSheet.Range("A1") = MyName
Open Range("B13") For Append As #1
Print #1, Cells(1, i).Value & MyName
i = i + 1
Close #1
' End If
End If
MyName = Dir 'search next directory
Loop
目前上述代码有问题,那个Cells(1, i).Value&; MyName是没有成功的,是过了Cells(1, i).Value = MyName,无论如何都没有成功,其他的都是对的.
3Private Sub Button_Submit_Click()
Dim shp As Shape
Sheets("sheet1").Copy
For Each shp In ActiveSheet.Shapes
shp.Delete
Next
'save as a file
ActiveWorkbook.SaveAs Filename:=Range("B13"), FileFormat:=xlExcel8
ActiveWorkbook.Close
MsgBox "success"
'read files
Dim MyName, MyFile, MyPath
Dim i&
i = 1
MyPath = Sheets("sheet1").[B11] & "\"
MyName = Dir(MyPath, vbDirectory)
Set wb = Workbooks.Open(Range("B13"))
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
'If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
'ActiveSheet.Range("A1") = MyName
'Open Range("B13") For Append As #1
'Print #1, Sheets("sheet1").Cells(1, i).Value = MyName
'With ThisWorkbook.Sheets(1)
wb.Sheets(1).Cells(1, i) = MyName
i = i + 1
' Close #1
' End If
'End With
End If
MyName = Dir 'search next directory
Loop
wb.Close
End Sub
三月二十八日更新内容如下:
Dim shp As Shape
Sheets("sheet1").Copy
For Each shp In ActiveSheet.Shapes
shp.Delete
Next
'save as a file
ActiveWorkbook.SaveAs Filename:=Range("B13"), FileFormat:=xlExcel8
ActiveWorkbook.Close
MsgBox "success"
'read files
Dim MyName, MyFile, MyPath
Dim i&
i = 1
MyPath = Sheets("sheet1").[B11] & "\"
MyName = Dir(MyPath, vbDirectory)
Set wb = Workbooks.Open(Range("B13"))
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
'If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
'ActiveSheet.Range("A1") = MyName
'Open Range("B13") For Append As #1
'Print #1, Sheets("sheet1").Cells(1, i).Value = MyName
'With ThisWorkbook.Sheets(1)
wb.Sheets(1).Cells(1, i) = MyName
i = i + 1
' Close #1
' End If
'End With
End If
MyName = Dir 'search next directory
Loop
wb.Close
End Sub
三月二十八日更新内容如下:
由于上述代码的写入问题有误,后是用set来设置文件对象比较好用,可以将指定的内容写入指定路径下的制定文件的单元格内,如下代码成功解决了上述问题:
Private Sub Button_Submit_Click()
Dim shp As Shape
Sheets("sheet1").Copy
For Each shp In ActiveSheet.Shapes
shp.Delete
Next
'save as a file
ActiveWorkbook.SaveAs Filename:=Range("B13"), FileFormat:=xlExcel8
ActiveWorkbook.Close
MsgBox "success"
'read files
Dim MyName, MyFile, MyPath
Dim i&
i = 1
MyPath = Sheets("sheet1").[B11] & "\"
MyName = Dir(MyPath, vbDirectory)
Set wb = Workbooks.Open(Range("B13"))
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
'If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
'ActiveSheet.Range("A1") = MyName
'Open Range("B13") For Append As #1
'Print #1, Sheets("sheet1").Cells(1, i).Value = MyName
'With ThisWorkbook.Sheets(1)
wb.Sheets(1).Cells(1, i) = MyName
i = i + 1
' Close #1
' End If
'End With
End If
MyName = Dir 'search next directory
Loop
wb.Close
End Sub
Dim shp As Shape
Sheets("sheet1").Copy
For Each shp In ActiveSheet.Shapes
shp.Delete
Next
'save as a file
ActiveWorkbook.SaveAs Filename:=Range("B13"), FileFormat:=xlExcel8
ActiveWorkbook.Close
MsgBox "success"
'read files
Dim MyName, MyFile, MyPath
Dim i&
i = 1
MyPath = Sheets("sheet1").[B11] & "\"
MyName = Dir(MyPath, vbDirectory)
Set wb = Workbooks.Open(Range("B13"))
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
'If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
'ActiveSheet.Range("A1") = MyName
'Open Range("B13") For Append As #1
'Print #1, Sheets("sheet1").Cells(1, i).Value = MyName
'With ThisWorkbook.Sheets(1)
wb.Sheets(1).Cells(1, i) = MyName
i = i + 1
' Close #1
' End If
'End With
End If
MyName = Dir 'search next directory
Loop
wb.Close
End Sub
0 0
- VBA学习笔记(2)之文件操作
- VBA 学习笔记 2
- Excel-VBA操作文件四大方法之四(2)
- Excel-VBA文件操作2
- Excel VBA 学习笔记(2)
- VBA入门学习笔记2
- VBA学习笔记(1)
- C++学习笔记之对文件的操作<2>
- Linux学习笔记(3)之文件操作
- ios学习笔记之文件操作(NSFileManager)
- JavaScript学习笔记之操作文件(第五天)
- php学习笔记之文件操作
- Objective-C学习笔记-之操作文件
- c++学习笔记之文件操作
- c语言学习笔记之文件操作
- Python学习笔记之简单文件操作
- Python学习笔记之文件操作总结
- scala学习笔记之文件IO操作
- windows远程桌面_保存密码
- andbase学习笔记一
- Android 颜色渲染(十) ComposeShader组合渲染
- WAMP配置php.ini去掉警告信息(NOTICE)的方法详解
- 论反馈信息如何推动 IT 运维团队进步?
- VBA学习笔记(2)之文件操作
- 数据结构学习之选择排序
- Vim 复制粘帖格式错乱问题的解决办法
- (初级)单兵开发者开发环境最佳实践(Android+JavaWeb)
- Scanner ,Math.random()答题正确统计
- 预览 doc docx,xls,xlsx,pdf等API 说明文档
- 用Python玩数据-笔记二
- JS实现GridView式的数据加载(带滚动条)
- 【iOS学习】十一、ActionSheet