Vba 连接到下一个循环

Vba 连接到下一个循环,vba,excel,concatenation,Vba,Excel,Concatenation,我有一个Excel电子表格,包含37列,13580列。我的目标是将相同的信息导出到记事本中。根据我所做的研究。这是不可能的,因为字符限制为240,而我的电子表格最多为256 是什么让这比它需要的困难得多,是我不能抓取“A1:AK1”并粘贴它。我希望每个列和字符串都有一些要求。例如: 我希望我导出的文本都是大写的。(要在Word到Excel再到Word之间复制粘贴以获得结果太麻烦了。数据的大文本可能会导致延迟。) 过程也一样) 每列都有一个最大字符限制(它们不都包含相同的限制,每一列可能不同于最

我有一个Excel电子表格,包含37列,13580列。我的目标是将相同的信息导出到记事本中。根据我所做的研究。这是不可能的,因为字符限制为240,而我的电子表格最多为256

是什么让这比它需要的困难得多,是我不能抓取“A1:AK1”并粘贴它。我希望每个列和字符串都有一些要求。例如:

  • 我希望我导出的文本都是大写的。(要在Word到Excel再到Word之间复制粘贴以获得结果太麻烦了。数据的大文本可能会导致延迟。) 过程也一样)
  • 每列都有一个最大字符限制(它们不都包含相同的限制,每一列可能不同于最后一列)
  • 我不需要分隔符,但是必须自动插入一个分隔符,并且根据字符限制的不同,分隔符的大小也不同(列必须在记事本中对齐,以便正确导入)
  • 如果可能的话,这是额外的,但是如果可以包括
    Trim()
    函数,以防某些值中包含多余的空格
  • 哦,我差点忘了,一些在记事本中对齐的值是左对齐的,还有一些是右对齐的。(举例如下)
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
最后,我得到的只是一个错误,因为所有东西都在一条线上,我不太确定如何分解它

另外,代码的每一部分,除了我粘贴的是/是通用的之外,我在无数来源的研究中都发现了它们。它只是简单地说明了导出函数/脚本

可以连接这样一个大的宏吗?如果没有,是否有我可以使用的循环? 我所需要的只是将其复制粘贴到记事本中,但要相应地隔开(不能依赖于列宽,因为我不太确定在某个参数中可以容纳多少个字符)

谢谢你


编辑 好的,我要感谢@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
但我得到的结果是它的走向,而不是更多的跨越。。。就像第一个环节

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

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

我做错了什么,我不知道如何修复它,或者为什么它看起来像这样。。。代码中似乎有额外的换行符,但我只插入了一次换行符

您必须将这一行代码分解为更简单的步骤

编写一个函数,该函数接受一个字符串并按您想要的方式处理它

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

希望这是有帮助的

这似乎足够简单的工作,感谢您的快速回复。我现在正试图实现代码,但有一个问题困扰着我。。现在,我仍然被归类为编码初学者,如何让
case2,3'列“B”和“C”tempString=inputString&Space(14-Len(inputString))
处理动态变量,如当前列/行?在我的示例中,我使用
Range.Cells
属性,它允许您为要引用的行/列传递一个整数
myRange。单元格(1,3)
将返回
myRange
第一行第三列中的单元格。太棒了,非常感谢你们的投入,以及对我的关注和问题的回答。我相信我正确地修改了该函数,以便在不引起任何问题的情况下使用我的电子表格。有两件事。。。我对UDF有点怀疑。我做得对吗
tempString=inputString&Range(colNum&lgl)
或者我应该使用
tempString=inputString&Range(colNum&i)
来代替?我在
oneLine=oneLine&ProcessCell(rngSelect.Cells(lgl,i),i)上得到一个对象定义错误。
我已经尝试将
oneLine
定义为字符串、变量和长字符串。。。什么都没用,我错过了什么。这是目前唯一一段不起作用的代码。(代码的主要核心)对于您的第一个问题,按照我编写的方式,
ProcessCell
没有cell的概念,它只是根据您的规则添加空格并返回该字符串。