[a1:b4].Select
Dim i As Long, irow As Long, S As String
Const NName As String = "C:\共享\shuju.txt" '要保存文件的位置
Open NName For Output As #1 '以读写方式打开文件,每次写内容都会覆盖原先的内容\
irow = [a65536].End(xlUp).Row '工作表里最后一列
irow1 = [b65536].End(xlUp).Row
If irow >irow1 Then
irow = irow
Else
irow = irow1
End If
For i = Selection.Row To Selection.Rows.Count + Selection.Row - 1 '开始循环写入数据
For j = 1 To Selection.Columns.Count
S = S &Cells(i, j)
Next
Print #1, S '把数据写到文本文件里
S = ""
Next i'写入下一个
Close #1 '关闭文件
MsgBox "工作表到文本文件转换完成!", 65, "提示"
End Sub
答:end()里面的数字是简写的,这样很不好辨认。强烈建议使用命名参数。
1、2、3、4代表xlToLeft、xlToRight、xlUp、xlDown。也就是点一个单元格,然后按“Ctrl+箭头”后指向的单元格。
我修改了这段代码,已验证了能正确导出。
Private Sub export()Application.ScreenUpdating = False
Path = "E:\export"
Dim nro&, nco&
nco = Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To nco
nro = Cells(Rows.Count, i).End(xlUp).Row
Open Path & "\file" & i & ".txt" For Output As #1
For Each cell In Range(Cells(1, i), Cells(nro, i))
Print #1, cell
Next
Close #1
Next
Application.ScreenUpdating = True
MsgBox "导出完成"
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)