在VBA中,不能将字符串值设为空
在Excel中在VBA中,不能将字符串值设为空,vba,excel,excel-formula,Vba,Excel,Excel Formula,在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 并且输出应该在记事本中 AN201@#BOS@#3061234@#035Yes@#102Yes@
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
并且输出应该在记事本中
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设置空值以还原新值
感谢您的及时回复!!谢谢在一维阵列上尝试连接并替换
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
您最初使用的
空格(n)
与我有关。这通常用于固定宽度的文本文件。非常好。。它工作正常…但标题也在显示您的示例数据没有标题,但对于i=2到最后一行,可能会解决问题。
是的,这是我的错。。无论如何,一切正常…感谢您的即时帮助