它只有一列,每列包含11个字符,没有换行符。 我找到了关于如何导出到txt的多个代码,但是我无法找到如何将行总数限制为500并以新的文本文件开头。有人可以帮忙吗?
所以要清楚。我想将500行导出到txt。然后下一个500到新的文本文件。 所有空行都已删除,并确保每行包含11个字符。
Sub saveText2()
Dim filename As String, lineText As String
Dim myrng As Range, i, j
filename = ThisWorkbook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt"
Open filename For Output As #1
Set myrng = Range("data")
For i = 1 To myrng.Rows.Count
For j = 1 To myrng.Columns.Count
lineText = IIf(j = 1, "", lineText & ",") & myrng.Cells(i, j)
Next j
Print #1, lineText
Next i
Close #1
End Sub
答案 0 :(得分:0)
我使用此代码
Sub setRangToCsv()
Dim R As Long, c As Integer
Dim filename As String, i As Long
R = Range("a" & Rows.Count).End(xlUp).Row
c = ActiveSheet.UsedRange.Columns.Count
For i = 1 To R Step 500
filename = ThisWorkbook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & "_" & Format(i, "0000") & ".txt"
TransToCSV filename, Range("a" & i).Resize(500, c)
Next i
End Sub
Sub TransToCSV(myfile As String, rng As Range)
Dim vDB, vR() As String, vTxt()
Dim i As Long, n As Long, j As Integer
Dim objStream
Dim strFile As String
Set objStream = CreateObject("ADODB.Stream")
vDB = rng
For i = 1 To UBound(vDB, 1)
n = n + 1
ReDim vR(1 To UBound(vDB, 2))
For j = 1 To UBound(vDB, 2)
vR(j) = vDB(i, j)
Next j
ReDim Preserve vTxt(1 To n)
vTxt(n) = Join(vR, ",")
Next i
strtxt = Join(vTxt, vbCrLf)
With objStream
'.Charset = "utf-8"
.Open
.WriteText strtxt
.SaveToFile myfile, 2
.Close
End With
Set objStream = Nothing
End Sub