从一个 Sheet 映射到另一个 Fill Unfilled 字符

Mapping from One Sheet to Another Fill Unfilled chracter

所以我有一个 excel 文档,其中包含部分填充的字符数组(“|”显示 excel 列行):

当前结果(“_”是 space):

1111GGH80100022190
1112QQH80100023201
1113GGH80100045201
1114AAH80100025190

所以我当前的代码输出了上面的结果。问题是字符 1-521-24 被跳过了。一般来说,如果没有列号,我应该打印“”(Space)。

期望的结果(“_”是 space):

_____1111GGH80100022____190
_____1112QQH80100023____201
_____1113GGH80100045____201
_____1114AAH80100025____190

是否有列方法可以检测我是否缺少 header 列中的范围?我目前使用这个并且只使用 select 数据:

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回答后------------ ------------------

所以代码可以完美运行,除非你有 header 那是一个数字 '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

--------------------------------错误快照------------ ----------------------

输入:

这是内置额外功能的代码。它实际上只是构建一个数组,将每个字符添加到正确的位置,然后将它们全部添加在一起,用空格替换空格。您必须更改最大值(在本例中为 hard-coded 27),但您可以使用与我用来获取 "intEnd" 相同的逻辑,通过遍历标题列来找到最大值。注意,这将补偿乱序列,我假设选择包括 headers(因此从 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