从一张纸到另一张纸的映射填充未填充的字符

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

Excel的屏幕截图

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

1111GGH80100022190 1112QQH80100023201 1113GGH80100045201 1114AAH80100025190 

所以我现在的代码输出这个结果。 问题是字符1-521-24被跳过。 一般来说,如果没有列号占我应该打印“”(空间)。

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

 _____1111GGH80100022____190 _____1112QQH80100023____201 _____1113GGH80100045____201 _____1114AAH80100025____190 

是否有列的方式来检测,如果我缺less标题列中的范围? 我目前使用这个,只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答案—————— ————–

因此,代码完美的作品,除非你有一个单一的数字'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 

——————————–错误快照—————- ——————

input:

在这里输入图像说明

这里是您的代码,内置了额外的function。它基本上只是build立一个数组,将每个字符添加到正确的位置,然后将它们一起添加,用空格replace空格。 你将不得不改变最大值(在这个例子中它是硬编码为27),但你可以使用相同的逻辑,我得到“intEnd”通过循环的标题列find你的最大值。 注意,这将补偿无序列,我假设select包括标题(因此从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 
Interesting Posts