【问题标题】:Mapping from One Sheet to Another Fill Unfilled chracter从一张纸映射到另一张填充未填充字符
【发布时间】:2023-04-10 07:27:01
【问题描述】:

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

当前结果(“_”是空格):

1111GGH80100022190
1112QQH80100023201
1113GGH80100045201
1114AAH80100025190

所以我当前的代码输出了上面的结果。问题是字符 1-521-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

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

输入:

【问题讨论】:

  • 是否可以假设标题中的每个数字对应一个字符?
  • 你能澄清你问题的开头吗?我在您写的内容中没有看到字符 1-5。我完全不明白你要做什么。
  • @DavidGM 和 TMH885 为了清晰起见,我添加了屏幕截图和更全面的结果。再次感谢您的时间!
  • 我想我明白你想要做什么。我只是不明白问题是什么。跳过1-5是什么意思?另外这句话是什么意思:是否有列方法来检测我是否在标题列中缺少范围?
  • @TMH8885 和 DavidGM 我澄清了当前和期望的结果。

标签: excel printing header mapping vba


【解决方案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

【讨论】:

  • 这太棒了!非常感谢!一个快速澄清:如何仅使用 1 个标题编号?据我了解,您的代码适用于查找 intBeg (25) 和 int End(27),但是如果标题是一个数字 63 怎么办?我尝试了 Len() if 循环。检查我发布的代码。
  • 我会使用 IF 语句,我会在可能的情况下编辑我的答案。
  • 啊,我明白了。如果在哪里结束?
  • Next j之前的行
  • 您是否将最大值(在 Dim strArr(1 to 27) 中)从 27 更改为 63?
猜你喜欢
  • 2011-06-10
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多