EXCEL宏程序求教

来源:百度知道 编辑:UC知道 时间:2024/06/05 14:00:41
N个TXT文件,用EXCEL逐个导入进行处理后保存为.XLS文件。请问可否用宏程序批量批量完成。导入和保存的程序怎么写?
txt:
--------|--------|--------|------------|
| 2. | 1989.20| 1992.00| 2.80| 1990.00| 82.10| 80.50| 31.82| 193.04| 9.51| 45.53| 10.31| 0.60| 8.17| 8.13|xx |
|-------|--------|--------|--------|--------|--------|--------|--------|--------|--------|--------|--------|--------|--------|--------|------------|
| 3. | 2008.00| 2009.60| 1.60| 2009.00| 44.93| 42.54| 27.94| 220.11| 25.01| 52.68| 15.23| 10.59| 48.13| 13.94|
以“|”为分隔,导入EXCEL后,去掉内容为“------”的行,保存成相同文件名的.xls文件即可。

txt文件名是固定的呢?还是不固定的?或者是有某种规律的?
==========================
文件数量固定吗?

另:能加点分吗?呵呵

===========================
导入后要做哪些处理,要不你写出来,这样VBA就可自动导入、处理、保存,一次性完成了

存放目录不一样的话请自行修改txtpath=这一句。
===============================
Sub 批量处理文本()
txtpath = "D:\data\"
txtfile = Dir(txtpath + "*.txt")
Do While txtfile <> ""
Workbooks.OpenText Filename:=txtpath + txtfile, DataType:=xlDelimited, Other:=True, OtherChar:="|"
Do
Set c = Cells.Find(What:="--", LookAt:=xlPart, MatchByte:=False)
If Not c Is Nothing Then
c.Activate
Selection.EntireRow.Delete
Else
Exit Do
End If
Loop
xlsfile = txtpath + Left(txtfile, Len(txtfile) - 3) + "xls"
ActiveWorkbook.SaveAs Filename:=xlsfile, FileFormat:=xlNormal
ActiveWindow.Close
txt