Macro not iterating through each sheet

djedidiahw007

New Member
Joined
Sep 19, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I can seem to figure out why my code is not iterating through each worksheet in the workbook.

Hopefully someone can help me out here.


VBA Code:
Sub CopytoWord()

Dim WordApp As Word.Application
Dim DocLoc As String
Dim LastRow As Long
Dim ws As Worksheet

  
   ' Begin the loop.
   
       
       
    For Each ws In ActiveWorkbook.Worksheets
    
        LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
        ws.Range("A1:F" & LastRow).Copy
        DocLoc = Range("G2").Value
        Set WordApp = CreateObject("word.application")
        WordApp.Documents.Open DocLoc
        WordApp.Visible = True
        WordApp.ActiveDocument.Activate
        WordApp.Application.Selection.MoveDown unit:=wdLine, Count:=1000
        
        With WordApp.Selection
            .PasteExcelTable LinkedToExcel:=False, WordFormatting:=True, RTF:=True
        End With
        
        WordApp.ActiveDocument.Close SaveChanges:=wdSaveChanges
        WordApp.Application.Quit
    
            
    Next ws
        
        
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try "ThisWorkbook.Worksheets".
If you want to loop through active workbook only, just "Worksheets" will be enough.
 
Upvote 0
Probably you need a ws. in front of

VBA Code:
Range("G2").Value
 
Upvote 0
Solution

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top