我有两个工作表。 sheet1具有数据,而sheet2具有最大长度 每个单元格引用sheet1中的数据。
我可以循环一次但不能在循环中循环第二组消息
Sheet1从第1行到第6行以及第7行到第12行有2组数据 https://i.stack.imgur.com/Jyids.jpg
Sheet2具有最大字符长度,必须用于第7行到第12行 https://i.stack.imgur.com/7JbBC.jpg
输出应该是这样的 https://i.stack.imgur.com/p9dQ5.jpg
Sheet1第1行到第6行引用了sheet2,但是我无法循环它 7至12的参考。
请注意 在记事本中,EODR后应打印第二条消息。
我的代码是, Sub我自己()
Dim str As String
Dim MaxStrLen As String
Dim rest As Integer
Dim Lstr As Integer
Dim LMstr As Integer
Dim MStr As Integer
Dim LR As Range
Dim CNT As Integer
Dim LastRow As Long
Dim LastCol As Long
Dim LRow As Long
Dim LCol As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim i As Long
Dim j As Long
Dim h As Long
Dim k As Long
Dim FilePath As String
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
Set ws3 = Sheets("Sheet3")
Open "C:\Users\Antony\Music\Excel Macros\Test.txt" For Output As #2
'''''''''''''''''''&#39
With ws1
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
For i = 1 To LastRow
sOut = vbNullString
For j = 1 To LastCol
str = .Cells(i, j).Value
MStr = ws2.Cells(i, j).Value
Lstr = Len(str)
rest = MStr - Lstr
sOut = sOut & str & Space(rest)
Next
Print #2, sOut
Next
End With
''''''''''''''''''<
With ws3
LRow = .Cells(.Rows.Count, 1).End(xlUp).Row
LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
slast = vbNullString
For k = 2 To LRow
str = Join(Application.Transpose(Application.Transpose(.Cells(k,
"A").Resize(1, LastCol).Value)), "@#")
str = Replace(str, "=", vbNullString)
Print #2, str
Next
Endtext = "EODR"
Print #2, slast & Endtext
End With
Close #2
End Sub
如果有更多消息,我需要循环,请帮我解决
答案 0 :(得分:0)
解决此问题
Sheet1第1行到第6行引用了sheet2,但是我无法为7到12循环相同的引用。
以这种方式修改中间块:
BlkSize=6 ' your data consists of blocks of 6 rows
For i = 1 To LastRow
sOut = vbNullString
LengthRow = i
Do While LengthRow > BlkSize
LengthRow = LengthRow - BlkSize
Loop
' now LengthRow points to row where char length is to be taken from
For j = 1 To LastCol
str = .Cells(i, j).Value
MStr = ws2.Cells(LengthRow, j).Value
Lstr = Len(str)
rest = MStr - Lstr
sOut = sOut & str & Space(rest)
Next
Print #2, sOut
Next