Results 1 to 6 of 6

Thread: Common Programming Pattern References

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,894
    Location
    Skeleton of approach

    Didn't do anything actually using Word, but you can see where you'd include your code

    Option Explicit
    
    
    Sub List2Word()
        Dim rList As Range, rList1 As Range
        Dim i As Long, j As Long
        Dim sPretendWordDoc As String
        Dim aryList As Variant, v As Variant
        
        'sort inputs
        With ActiveSheet
            Set rList = .Range("A1").CurrentRegion
            Set rList1 = rList.Cells(2, 1).Resize(rList.Rows.Count - 1, rList.Columns.Count)
    
    
            
            With .Sort
                .SortFields.Clear
                .SortFields.Add Key:=rList1, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            
                .SetRange rList
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        End With
        
        'make into array
        aryList = Application.WorksheetFunction.Transpose(rList.Columns(1))
         
        'combine same values
        For i = UBound(aryList) To LBound(aryList) + 1 Step -1
            If aryList(i) = aryList(i - 1) Then
                aryList(i - 1) = aryList(i - 1) & Chr(1) & aryList(i)   '   Chr(1) is just marker
                aryList(i) = Empty
            End If
        Next i
            
        'do wonderful stuff with MS Word
        For i = LBound(aryList) + 1 To UBound(aryList)
            If Not IsEmpty(aryList(i)) Then
                
                sPretendWordDoc = "The Word doc will use ... " & vbCrLf & vbCrLf
                
                v = Split(aryList(i), Chr(1))
                For j = LBound(v) To UBound(v)
                    sPretendWordDoc = sPretendWordDoc & vbTab & v(j) & vbCrLf
                Next j
    
    
                MsgBox sPretendWordDoc
            End If
        Next i
    
    
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •