Next without for Error

Likith

New Member
Joined
Dec 26, 2018
Messages
9
Hello ,

I am trying to merge few sheets from other works into Master Workbook using following code but Its showing " Compile Error : Next without for "

Do you have any idea about this and suggest some Ideas

Code:
Sub MergeSheets2()
Dim xStrPath As String
Dim xStrFName As String
Dim xWS As Worksheet
Dim xMWS As Worksheet
Dim xTWB As Workbook
Dim xStrAWBName As String
Dim xI As Integer
On Error Resume Next
 
xStrPath = "C:\Users\reiu079\Desktop\09-07-19\Work Plans -FY20"
xStrName = "Performance,Development,Profile"
 
xArr = Split(xStrName, ",")
 
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set xTWB = ThisWorkbook
xStrFName = Dir(xStrPath & "*.xlsx")
Do While Len(xStrFName) > 0
Workbooks.Open Filename:=xStrPath & xStrFName, ReadOnly:=True
xStrAWBName = ActiveWorkbook.Name
For Each xWS In ActiveWorkbook.Sheets
For xI = 0 To UBound(xArr)
If xWS.Name = xArr(xI) Then
xWS.Copy After:=xTWB.Sheets(xTWB.Sheets.Count)
Set xMWS = xTWB.Sheets(xTWB.Sheets.Count)
xMWS.Name = xStrAWBName & "(" & xArr(xI) & ")"
Exit For
Next xI
End If
Next xWS
Workbooks(xStrAWBName).Close
xStrFName = Dir()
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub

Thanks for your help in advance !!
 
Last edited by a moderator:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
If you indent your code it's easier to find problems like this.
You have the End If in the wrong place
Code:
Sub MergeSheets2()
   Dim xStrPath As String
   Dim xStrFName As String
   Dim xWS As Worksheet
   Dim xMWS As Worksheet
   Dim xTWB As Workbook
   Dim xStrAWBName As String
   Dim xI As Integer
   On Error Resume Next
    
   xStrPath = "C:\Users\reiu079\Desktop\09-07-19\Work Plans -FY20"
   xStrName = "Performance,Development,Profile"
    
   xArr = Split(xStrName, ",")
    
   Application.ScreenUpdating = False
   Application.DisplayAlerts = False
   Set xTWB = ThisWorkbook
   xStrFName = Dir(xStrPath & "*.xlsx")
   Do While Len(xStrFName) > 0
      Workbooks.Open Filename:=xStrPath & xStrFName, ReadOnly:=True
      xStrAWBName = ActiveWorkbook.Name
      For Each xWS In ActiveWorkbook.Sheets
         For xI = 0 To UBound(xArr)
            If xWS.Name = xArr(xI) Then
               xWS.Copy After:=xTWB.Sheets(xTWB.Sheets.Count)
               Set xMWS = xTWB.Sheets(xTWB.Sheets.Count)
               xMWS.Name = xStrAWBName & "(" & xArr(xI) & ")"
               Exit For
      [COLOR=#ff0000]      End If
         Next xI
      Next xWS[/COLOR]
      Workbooks(xStrAWBName).Close
      xStrFName = Dir()
   Loop
   Application.ScreenUpdating = True
   Application.DisplayAlerts = True
End Sub
 
Upvote 0
When you nest things, you must close out the most recent thing first. Note this section:
Code:
    For Each xWS In ActiveWorkbook.Sheets
        For xI = 0 To UBound(xArr)
            If xWS.Name = xArr(xI) Then
                xWS.Copy After:=xTWB.Sheets(xTWB.Sheets.Count)
                Set xMWS = xTWB.Sheets(xTWB.Sheets.Count)
                xMWS.Name = xStrAWBName & "(" & xArr(xI) & ")"
                Exit For
[COLOR=#ff0000]             Next xI
        End If[/COLOR]
    Next xWS
So, the "Next xI" and "End If" lines need to be reversed.
 
Upvote 0

Forum statistics

Threads
1,214,630
Messages
6,120,634
Members
448,973
Latest member
ChristineC

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