当前位置:编程学习 > VB >>

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

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列数据 放在记事本中?谢谢各位大虾。。 --------------------编程问答-------------------- 这个还用编程啊。直接复制粘贴就好了 --------------------编程问答--------------------
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 代码 --------------------编程问答-------------------- Columns("E:G").Delete
Columns("A:B").Delete
ActiveWorkbook.SaveAs Filename:="D:\t.txt", FileFormat:=xlText, CreateBackup:=False

用VBA删除几列,然后另存为txt文件就行了。 --------------------编程问答-------------------- 很好,支持一下 --------------------编程问答-------------------- 除
补充:VB ,  VBA
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,