How to make loop stop when the cell is empty? (VBA)

feni1388

Board Regular
Joined
Feb 19, 2018
Messages
133
Office Version
  1. 2021
Platform
  1. Windows
Hello....

Perhaps some of the code that I'm running is incorrect, but the loop that I made didn't stop when the cell is empty --> cell (i, 29)
Can you someone please help?

VBA Code:
 Dim sFolderPath As String
    Dim sFileName As String, sFilePath As String
    Dim wbName As String
    
    
    'Define Folder Path
    sFolderPath = "\\192.168.1.101\Share\admin\planet\"
      
            
      
    'Check Specified File exists or not
    
    For i = 2 To 8
        wbName = "planetupload" & Cells(i, 29).Value & ".xlsm"
    
    sFilePath = sFolderPath & wbName
    
    
    If Dir(sFilePath) <> "" Then
        Workbooks.Open Filename:="\\192.168.1.101\Share\admin\planet\" & wbName
        
        ThisWorkbook.Activate
        Sheets("futuredate").Select
        Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Application.CutCopyMode = False

        ActiveWorkbook.Close (True)
        
        
    Else
    
    'If file is not available
    
    Workbooks.Open Filename:="\\192.168.1.101\Share\admin\planet\planetupload" & ".xlsm"
    
    ActiveWorkbook.SaveAs sFolderPath & wbName
    
       Workbooks("planetupload" & Format(Date, "yyyymmdd") & ".xlsm").Activate
        Sheets("copy").Select
        If Not Sheets("copy").AutoFilterMode Then Range("A1:Z1").AutoFilter
        ActiveSheet.Range("$A$1:$Y$501").AutoFilter Field:=26, Criteria1:=Cells(i, 29)
        Columns("A:Y").Select
        Selection.Copy
        
        Workbooks(wbName).Activate
        Sheets("futuredate").Select
        Columns("A:Y").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Application.CutCopyMode = False
      
      
      
    
    
    End If
    
    Next i
    
    

    MsgBox "Future date template is complete"



ActiveWorkbook.Save
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Change this line
VBA Code:
For i = 2 To 8
 wbName = "planetupload" & Cells(i, 29).Value & ".xlsm"
As
VBA Code:
For i = 2 To 8
If Cells(i, 29)<>"" then
 wbName = "planetupload" & Cells(i, 29).Value & ".xlsm"
....full code
end if
Next i
 
Upvote 0
Solution
Change this line
VBA Code:
For i = 2 To 8
 wbName = "planetupload" & Cells(i, 29).Value & ".xlsm"
As
VBA Code:
For i = 2 To 8
If Cells(i, 29)<>"" then
 wbName = "planetupload" & Cells(i, 29).Value & ".xlsm"
....full code
end if
Next i
Thank you.... It works perfectly OK.
 
Upvote 0

Forum statistics

Threads
1,223,883
Messages
6,175,167
Members
452,615
Latest member
bogeys2birdies

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