+ -
当前位置:首页 → 问答吧 → Excel用VBA 处理数据 取其中几列的数据以TXT格式存放

Excel用VBA 处理数据 取其中几列的数据以TXT格式存放

时间:2011-09-06

来源:互联网

Excel用VBA 处理数据 取其中几列的数据以TXT格式存放 ,
X45 S1-1 876 14.6 0 0 0 0 0 0 0
X45 S2-1 890.6 2.8 0.2 0 0 1 -999 -999 0
X45 S2-1-1 893.4 2.4 1 -999 0 1 -999 -999 0
取3 4 8 9 10 11列数据 放在记事本中?谢谢各位大虾。。

作者: dingpeng0405   发布时间: 2011-09-06

这个还用编程啊。直接复制粘贴就好了

作者: zoujing1000   发布时间: 2011-09-07

VB code
Sub Macro10()
Dim st As Worksheet
Dim wk As Excel.Workbook
Set wk = Excel.ActiveWorkbook
For i = 1 To wk.Sheets.Count
   'MsgBox wk.Sheets(i).Name
   If Trim(wk.Sheets(i).Name) = "QPM" Then
      Set st = wk.Sheets(i)
   End If
Next i

Dim strpath As String
If Right(wk.Path, 1) = "\" Then
   strpath = wk.Path
Else
   strpath = wk.Path & "\"
End If

MsgBox st.UsedRange.Columns.Count
MsgBox st.UsedRange.Rows.Count

For i = 1 To st.UsedRange.Columns.Count
   For j = 1 To st.UsedRange.Rows.Count
     If IsNumeric(st.Cells(j, i)) = True Then
        If j >= 17 Then
           str1 = str1 & Format(st.Cells(j, i), "####0.0000") & vbTab
        Else
           str1 = str1 & st.Cells(j, i) & vbTab
        End If
     Else
        str1 = str1 & st.Cells(j, i) & vbTab
     End If
   Next j
   str1 = Left(str1, Len(str1) - 1)
   str1 = str1 & vbCrLf
Next i
str1 = Left(str1, Len(str1) - 2)

writefile命令
Dim fso As Object, f As Object
Set fso = CreateObject("scripting.filesystemobject")
Set f = fso.opentextfile(strpath & "LAT.txt", 2, True) '这里的8表示追加写入,如果是覆盖的话就用2
f.write str1
'f.writeline "xxx" '这个和print语句相似
f.Close

MsgBox "success10"


End Sub

给你一段vba写入 txt 代码

作者: yikeda   发布时间: 2011-09-07

热门下载

更多