在Excel中,
AN201 BOS 306=1234 035=Yes 102=Yes 100=70 097=Yes Sometext shouldcome after a longspace thankyou
AN201 BOS 306=1235 035=No 102=No 100=71 097=No This is second scenario thankyou
OUTPUT应该在NOTEPAD中,
AN201@#BOS@#3061234@#035Yes@#102Yes@#10070@#097Yes@#Sometext shouldcome after a longspace thankyou
AN201@#BOS@#3061234@#035Yes@#102Yes@#10070@#097Yes@#This is second scenario
thankyou
我尝试的脚本是,
Sub Characterpostion()
Dim FilePath As String
Dim CellData As String
Dim LastCol As Long
Dim LastRow As Long
Dim str As String
Dim cellvalue As String
LastCol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
FilePath = FreeFile
CellData = ""
Open "C:\Users\Antony\Music\Excel Macros\Text2.txt" For Output As #2
For i = 1 To LastRow
For j = 1 To LastCol
Select Case j
Case 1
n = 5 - Len(Trim(ActiveCell(i, j).Value))
Case 2
n = 3 - Len(Trim(ActiveCell(i, j).Value))
Case 3
n = 8 - Len(Trim(ActiveCell(i, j).Value))
Case 4
n = 7 - Len(Trim(ActiveCell(i, j).Value))
Case 5
n = 7 - Len(Trim(ActiveCell(i, j).Value))
Case 6
n = 6 - Len(Trim(ActiveCell(i, j).Value))
Case 7
n = 7 - Len(Trim(ActiveCell(i, j).Value))
End Select
CellData = CellData & Space(n) & Trim(ActiveCell(i, j).Value) & "@#"
If CellData Like "*=*" Then
Dim WrdArray() As String
WrdArray() = Split(CellData, "=")
str = WrdArray(0) + WrdArray(1)
Print #2 , str
Else
End If
Next j
Print #2 , CellData
CellData = ""
Next i
Close #2
End Sub
但CellData一次存储所有案例......我无法使CellData无效以恢复新值。
感谢您的即时回复!!感谢
答案 0 :(得分:1)
尝试在1-D阵列上进行连接并替换。
with ActiveSheet
For i = 1 To LastRow
str = join(application.transpose(application.transpose(.Cells(i, "A").resize(1, LastCol).Value)), "@#")
str = replace(str, "=", vbnullstring)
Print #2 , str
Next i
end with
您最初使用的Space(n)
关注我。这通常用于固定宽度的文本文件。