' Tools > References > Microsoft Word 14.0 Object Library > OK
Sub MacroStudent4()
'Step 1: Declare your variables
Dim MyRange As Excel.Range
Dim MyRange1 As Excel.Range
Dim MyCell As Excel.Range
Dim wd As Word.Application
Dim wdDoc As Word.Document
Dim WdRange As Word.Range
Dim wdTable As Word.Table
Dim wdBreak As Word.Break
Dim LastRow As Long
Dim LastColumn As Long
Dim fTable As Range, r As Range, c As Range
Dim nCols As Integer, i As Integer, nLastRow As Long
Set fTable = FindAll(ActiveSheet.UsedRange, "Table ", xlValues, xlPart)
'Set fTable = FoundRanges(ActiveSheet.UsedRange, "Table ", xlPart)
If fTable Is Nothing Then Exit Sub
'Open the target Word document
Set wd = New Word.Application
Set wdDoc = wd.Documents.Add 'create a new document
wd.Visible = True
'Set focus on the target
Set WdRange = wdDoc.Range
'Find last row of last table (last cell in column A with data).
nLastRow = Cells(Rows.Count, "A").End(xlUp).Row - 1 ' -1 since a date row is added
'Create a blank table in Word
For i = 1 To fTable.Cells.Count
'Set r = Cells(fTable.Areas(i).Row + 1, Columns.Count).End(xlToLeft)
'nCols = r.MergeArea.Cells.Count + Cells(fTable.Areas(i).Row + 1, Columns.Count).End(xlToLeft).Column - 1
nCols = LastColInRow(Cells(fTable.Areas(i).Row + 2, Columns.Count)) 'Check last column by 2 rows below Table's 1st row.
If i <> fTable.Cells.Count Then
Set r = Range(fTable.Areas(i), Cells(fTable.Areas(i + 1).Row - 1, nCols))
Else
Set r = Range(fTable.Areas(i), Cells(nLastRow, nCols))
End If
r.Copy
With wd.Selection
.Paste 'paste in the table
'Adjust column widths
.Tables(1).AutoFitBehavior wdAutoFitContent
.EndKey Unit:=wdStory
.TypeParagraph
End With
Next i
'Memory cleanup
Application.CutCopyMode = False
Range("A1").Select
Set wd = Nothing
Set wdDoc = Nothing
Set WdRange = Nothing
End Sub
' Chip Pearson, http://www.cpearson.com/excel/FindAll.aspx
Function FindAll(SearchRange As Range, _
FindWhat As Variant, _
Optional LookIn As XlFindLookIn = xlValues, _
Optional LookAt As XlLookAt = xlWhole, _
Optional SearchOrder As XlSearchOrder = xlByRows, _
Optional MatchCase As Boolean = False, _
Optional BeginsWith As String = vbNullString, _
Optional EndsWith As String = vbNullString, _
Optional BeginEndCompare As VbCompareMethod = vbTextCompare) As Range
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' FindAll
' This searches the range specified by SearchRange and returns a Range object
' that contains all the cells in which FindWhat was found. The search parameters to
' this function have the same meaning and effect as they do with the
' Range.Find method. If the value was not found, the function return Nothing. If
' BeginsWith is not an empty string, only those cells that begin with BeginWith
' are included in the result. If EndsWith is not an empty string, only those cells
' that end with EndsWith are included in the result. Note that if a cell contains
' a single word that matches either BeginsWith or EndsWith, it is included in the
' result. If BeginsWith or EndsWith is not an empty string, the LookAt parameter
' is automatically changed to xlPart. The tests for BeginsWith and EndsWith may be
' case-sensitive by setting BeginEndCompare to vbBinaryCompare. For case-insensitive
' comparisons, set BeginEndCompare to vbTextCompare. If this parameter is omitted,
' it defaults to vbTextCompare. The comparisons for BeginsWith and EndsWith are
' in an OR relationship. That is, if both BeginsWith and EndsWith are provided,
' a match if found if the text begins with BeginsWith OR the text ends with EndsWith.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim FoundCell As Range
Dim FirstFound As Range
Dim LastCell As Range
Dim ResultRange As Range
Dim XLookAt As XlLookAt
Dim Include As Boolean
Dim CompMode As VbCompareMethod
Dim Area As Range
Dim MaxRow As Long
Dim MaxCol As Long
Dim BeginB As Boolean
Dim EndB As Boolean
CompMode = BeginEndCompare
If BeginsWith <> vbNullString Or EndsWith <> vbNullString Then
XLookAt = xlPart
Else
XLookAt = LookAt
End If
' this loop in Areas is to find the last cell
' of all the areas. That is, the cell whose row
' and column are greater than or equal to any cell
' in any Area.
For Each Area In SearchRange.Areas
With Area
If .Cells(.Cells.Count).Row > MaxRow Then
MaxRow = .Cells(.Cells.Count).Row
End If
If .Cells(.Cells.Count).Column > MaxCol Then
MaxCol = .Cells(.Cells.Count).Column
End If
End With
Next Area
Set LastCell = SearchRange.Worksheet.Cells(MaxRow, MaxCol)
On Error GoTo 0
Set FoundCell = SearchRange.Find(what:=FindWhat, _
After:=LastCell, _
LookIn:=LookIn, _
LookAt:=XLookAt, _
SearchOrder:=SearchOrder, _
MatchCase:=MatchCase)
If Not FoundCell Is Nothing Then
Set FirstFound = FoundCell
Do Until False ' Loop forever. We'll "Exit Do" when necessary.
Include = False
If BeginsWith = vbNullString And EndsWith = vbNullString Then
Include = True
Else
If BeginsWith <> vbNullString Then
If StrComp(Left(FoundCell.Text, Len(BeginsWith)), BeginsWith, BeginEndCompare) = 0 Then
Include = True
End If
End If
If EndsWith <> vbNullString Then
If StrComp(Right(FoundCell.Text, Len(EndsWith)), EndsWith, BeginEndCompare) = 0 Then
Include = True
End If
End If
End If
If Include = True Then
If ResultRange Is Nothing Then
Set ResultRange = FoundCell
Else
Set ResultRange = Application.Union(ResultRange, FoundCell)
End If
End If
Set FoundCell = SearchRange.FindNext(After:=FoundCell)
If (FoundCell Is Nothing) Then
Exit Do
End If
If (FoundCell.Address = FirstFound.Address) Then
Exit Do
End If
Loop
End If
Set FindAll = ResultRange
End Function
Function LastColInRow(aCell As Range) As Integer
Dim LastCell As Range
Set LastCell = Worksheets(aCell.Parent.Name).Cells(aCell.Row, Worksheets(aCell.Parent.Name).Columns.Count)
Set aCell = LastCell
Do Until aCell.Column = 1 Or (LastCell.DisplayFormat.Interior.Color <> aCell.DisplayFormat.Interior.Color)
Set aCell = aCell.Offset(0, -1)
Loop
'LastColInRow = aCell.Column + aCell.MergeArea.Cells.Count - 1
LastColInRow = aCell.Column
End Function