이제 사용자 입력 데이터의 예를 제공하는 출력을 소망 한 것으로, 여기에서 이전에 제공된 코드에 저장하는 루틴을 수정하는 작업을 수행하는 한 가지 방법이다.
첫 번째 차원의 크기를 변경하는 배열을 Redim Preserve 할 수 없으므로 Collection 개체를 사용하여 모든 개별 줄을 수집 한 다음 결과 배열의 크기를 지정하고 별도의 단계로 채 웁니다.
일부 가정은 원본 데이터가 1 열 A 열에 C 열에 있고 결과가 sheet2에 기록된다는 것입니다.
코드를 살펴보고 각 단계에서 진행되는 작업을 이해하십시오. 뭔가가 왜 다른쪽으로 이루어 졌는지에 대해 명확하게 질문하십시오.
Option Explicit
Sub WordWrap2()
'requires reference to Microsoft VBScript Regular Expressions 5.5
'Wraps at W characters, but will allow overflow if a word is longer than W
Dim RE As RegExp, MC As MatchCollection, M As Match
Dim str As String
Const W As Long = 72
Dim wsSrc As Worksheet, wsRes As Worksheet, rRes As Range
Dim vSrc As Variant
Dim vRes() As Variant 'Results
Dim colLines As Collection
Dim vLine(1 To 3) As Variant 'to store each line in collection
Dim I As Long
'Set source to column A:C
' A = Sequence
' B = ID
' C = TXLINE2
Set wsSrc = Worksheets("sheet1")
Set wsRes = Worksheets("sheet2")
Set rRes = wsRes.Cells(1, 1)
With wsSrc
vSrc = .Range("A1", .Cells(.Rows.Count, "C").End(xlUp))
End With
Set RE = New RegExp
RE.Global = True
'Cycle through third column only, and collect the data
Set colLines = New Collection
For I = 1 To UBound(vSrc, 1)
str = vSrc(I, 3)
'remove all line feeds and nbsp
RE.Pattern = "[\xA0\r\n\s]+"
str = RE.Replace(str, " ")
RE.Pattern = "\S.{0," & W - 1 & "}(?=\s|$)|\S{" & W & ",}"
If RE.Test(str) = True Then
Set MC = RE.Execute(str)
'Collect the lines, along with Seq and ID
For Each M In MC
vLine(1) = vSrc(I, 1)
vLine(2) = vSrc(I, 2)
vLine(3) = M
colLines.Add vLine
Next M
Else 'Allow preservation of blank lines in source data
Erase vLine
colLines.Add vLine
End If
Next I
'create results array
ReDim vRes(1 To colLines.Count, 1 To 3)
'Note that column headers are carried over from source data
'Populate with the data
For I = 1 To colLines.Count
vRes(I, 1) = colLines(I)(1)
vRes(I, 2) = colLines(I)(2)
vRes(I, 3) = colLines(I)(3)
Next I
Set rRes = rRes.Resize(UBound(vRes, 1), UBound(vRes, 2))
With rRes
.EntireColumn.Clear
.Value = vRes
With .Rows(1)
.Font.Bold = True
.HorizontalAlignment = xlCenter
End With
.EntireColumn.AutoFit
End With
Set RE = Nothing
End Sub
전 :
후 :
당신이 코드를 수정하려고 할 때로 실행 않았다 어떤 문제
? 원하는 결과는 어떻게 생겼습니까? 귀하의 데이터는 어떻게 생겼습니까? [좋은 질문을하는 방법] (http://stackoverflow.com/help/how-to-ask)과 [최소한의 완전하고 검증 가능한 예제를 만드는 방법] (http : //stackoverflow.com/help/mcve –
@RonRosenfeld 그래, 노력하고있어.이 수준에서 코드를 수정하는 것은 꽤 어렵다. 나는 혼합 된 출력을 얻고있다. – Monty