所以我有一个excel文档,它有部分填充的字符数组(" |"显示excel列行):
当前结果(" _"是空格):
1111GGH80100022190
1112QQH80100023201
1113GGH80100045201
1114AAH80100025190
所以我当前的代码输出上面的结果。问题是, 1-5 和 21-24 字符会被跳过。一般来说,如果没有列号,我应该打印" " (空间)。
期望的结果(" _"是一个空格):
_____1111GGH80100022____190
_____1112QQH80100023____201
_____1113GGH80100045____201
_____1114AAH80100025____190
是否有列方式来检测我是否缺少标题列中的范围?我目前使用它并且只选择数据:
Private Sub WriteFile_Click()
Dim myFile As String, rng As Range, cellValue As Variant, I As Integer, j As Integer
myFile = "C:\Reformatted.txt"
Set rng = Selection
Open myFile For Output As #1
For I = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = cellValue + CStr(rng.Cells(I, j).Value)
cellValue = Replace(cellValue, "NULL", " ")
If j = rng.Columns.Count Then
Print #1, cellValue
End If
Next j
cellValue = ""
Next I
Close #1
Shell "C:\Windows\Notepad.exe C:\Reformatted.txt", 1
End Sub
----------------------------- TMh885之后回答--------------- -----------------
因此,如果您的标题包含一个数字,那么代码就可以完美地运行' 63'
所以我正在尝试这个:
Private Sub CommandButton1_Click()
Dim myFile As String, rng As Range, cellValue As Variant, I As Integer, j As Integer
myFile = "C:\Reformatted.txt"
Set rng = Selection
Open myFile For Output As #1
Dim strArr(1 To 63) As String, intBeg As Integer, intEnd As Integer, intCount As Integer
For I = 2 To rng.Rows.Count
For j = 1 To rng.Columns.Count
If Len(Cells) = 1 Then
cellValue = cellValue + " "
Else
intBeg = Val(Left(Cells(1, j).Value, InStr(1, Cells(1, j).Value, "-") - 1))
intEnd = Val(Right(Cells(1, j).Value, Len(Cells(1, j).Value) - InStr(1, Cells(1, j).Value, "-")))
intCount = 1
For t = intBeg To intEnd
strArr(t) = Mid(Cells(I, j).Value, intCount, 1)
intCount = intCount + 1
Next t
End If
Next j
For t = 1 To UBound(strArr)
If strArr(t) = "" Then strArr(t) = " "
cellValue = cellValue + strArr(t)
Next t
Erase strArr
Print #1, cellValue
cellValue = ""
Next I
Close #1
Shell "C:\Windows\Notepad.exe C:\Reformatted.txt", 1
End Sub
--------------------------------错误快照------------- ---------------------
输入:
答案 0 :(得分:1)
这里有你内置额外功能的代码。它基本上只是构建一个数组,将每个字符添加到正确的位置然后将它们全部加在一起,用空格替换空格。您必须更改最大值(在此示例中,它在27处硬编码)但您可以使用与以前相同的逻辑来获取" intEnd"通过循环标题列找到最大值。注意,这将补偿乱序列,我假设选择包括标题(因此从I = 2开始):
Private Sub WriteFile_Click()
Dim myFile As String, rng As Range, cellValue As Variant, I As Integer, j As Integer
myFile = "C:\Reformatted.txt"
Set rng = Selection
Open myFile For Output As #1
Dim strArr(1 To 27) As String, intBeg As Integer, intEnd As Integer, intCount As Integer
For I = 2 To rng.Rows.Count
For j = 1 To rng.Columns.Count
If InStr(1, CStr(Cells(1, j).Value), "-") = 0 Then
strArr(Val(Cells(1, j).Value)) = Cells(I, j).Value
Else
intBeg = Val(Left(Cells(1, j).Value, InStr(1, Cells(1, j).Value, "-") - 1))
intEnd = Val(Right(Cells(1, j).Value, Len(Cells(1, j).Value) - InStr(1, Cells(1, j).Value, "-")))
intCount = 1
For t = intBeg To intEnd
strArr(t) = Mid(Cells(I, j).Value, intCount, 1)
intCount = intCount + 1
Next t
End If
Next j
For t = 1 To UBound(strArr)
If strArr(t) = "" Then strArr(t) = " "
cellValue = cellValue + strArr(t)
Next t
Erase strArr
Print #1, cellValue
cellValue = ""
Next I
Close #1
Shell "C:\Windows\Notepad.exe C:\Reformatted.txt", 1
End Sub