|
本帖最后由 my6730c 于 2019-11-9 09:11 编辑
txt文件中有24,575,571.00-这种负数,宏命令导入excel之后负数会变成前面有空格的文本形如“ 24,575,571.00-”宏命令如下:
Sub import_from_txt3()
Sheets("利润表-当期").Activate
Dim file_name As String, my_path As String
Dim lines, cols
Dim i As Integer, j As Integer, k As Integer, q As Integer
Application.ScreenUpdating = False
Worksheets("利润表-当期").Range("A1:K150").ClearContents
my_path = ThisWorkbook.Path
file_name = "3.txt"
'读取文件
Open my_path & "\" & file_name For Input As #1
lines = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
Close #1
k = UBound(lines) + 1 '文件的行数
'遍历每一行
For i = 1 To k
cols = Split(lines(i - 1), "|") '以逗号作为分隔,将每行字符分割,分隔符可根据实际情况自己修改
q = UBound(cols) + 1 '分隔成的列数
For j = 1 To q '遍历该行的每一列
Worksheets("利润表-当期").Cells(i, j) = cols(j - 1) '输出到表格中
Next
Next
Application.ScreenUpdating = True
End Sub
|
|