+ Reply to Thread
Results 1 to 5 of 5

Need macro to end once blank is reached

Hybrid View

  1. #1
    Registered User
    Join Date
    09-04-2014
    Location
    Sydney, Australia
    MS-Off Ver
    2013
    Posts
    52

    Question Need macro to end once blank is reached

    Hi all, I have a macro that runs for values on my excel master list ranging from I = 2 to 151. For a future users reference I want to change that to
    For i = 2 To 300
    incase extra rows are needed to be added. The only thing is at the end of the 151th row, an error comes up (naturally) and I have to manually press end code. Can I insert an option where it will just close itself when it encounters a blank? Thanks in advance, macro_
    '==========>>
     Option Explicit
     '---------->>
     Public Sub PassVariables()
           Dim WB As Workbook
           Dim SH As Worksheet
           Set WB = ThisWorkbook
           Set SH = WB.Sheets("Sheet1")
           Dim i As Variant
           For i = 2 To 151
           
          With SH
           Call Main(myYear:=.Range("A2").Value, _
                     myQuarter:=CStr(.Range("B2").Value), _
                     myFolder:=CStr(.Range("C2").Value), _
                     mySaveAsFolder:=CStr(.Range("D" & i).Value), _
                     mySaveAsName:=CStr(.Range("E" & i).Value), _
                     blCreateFolder:=CStr(.Range("F" & i).Value))
          End With
     Next
    
     End Sub '---------->>
    
     '---------->>
     Public Sub Main(myYear As Variant, myQuarter As String, _
                        myFolder As String, _
                        mySaveAsFolder As String, _
                        mySaveAsName As String, _
                        Optional blCreateFolder As Boolean)
          Dim WB As Workbook
          Dim WS As Worksheet
          Dim spath As String
          Dim sSaveAsPath As String
          Dim sFilename As String
          Dim sFullname As String
          Dim aStr As String
         
          aStr = myQuarter & " " & myYear
          spath = "X:\SPECIFICFOLDER\" & myYear & "\" & aStr & "\TMT\" & myFolder
          sSaveAsPath = "X:\SPECIFICFOLDER\" & myYear & "\" & aStr & "\TMT\" & mySaveAsFolder
          sFilename = "ST" & aStr & ".xlsm"
          sFullname = spath & "\" & sFilename
          Workbooks.Open Filename:=sFullname, UpdateLinks:=0
          ActiveCell.Offset(-1, 0).FormulaR1C1 = mySaveAsName
          Set WS = ActiveSheet
          Set WB = Workbooks.Add(xlWBATWorksheet)
          WS.Range("A1:S84").Copy
          WB.Sheets(1).Range("A1").PasteSpecial Paste:=xlPasteValues
          WB.Sheets(1).Range("A1").PasteSpecial Paste:=xlPasteFormats
          Application.CutCopyMode = False
          
          If blCreateFolder Then
               MkDir sSaveAsPath
              blCreateFolder = False
          End If
    
          'ChDir sSaveAsPath
         With ActiveWorkbook
              .SaveAs Filename:=sSaveAsPath & "\" & mySaveAsName, _
                      FileFormat:=xlOpenXMLWorkbook, _
                      CreateBackup:=False
             .Close SaveChanges:=False
         End With
    
     End Sub
     '<<==========

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Need macro to end once blank is reached

    Just add in a condition:

    '==========>>
     Option Explicit
     '---------->>
     Public Sub PassVariables()
           Dim WB As Workbook
           Dim SH As Worksheet
           Set WB = ThisWorkbook
           Set SH = WB.Sheets("Sheet1")
           Dim i As Variant
           For i = 2 To 300 '151
           If .Range("D" & i).Value <> "" AND .Range("E" & i).Value <> "" AND .Range("F" & i).Value <> "" Then
          With SH
           Call Main(myYear:=.Range("A2").Value, _
                     myQuarter:=CStr(.Range("B2").Value), _
                     myFolder:=CStr(.Range("C2").Value), _
                     mySaveAsFolder:=CStr(.Range("D" & i).Value), _
                     mySaveAsName:=CStr(.Range("E" & i).Value), _
                     blCreateFolder:=CStr(.Range("F" & i).Value))
          End With
          Else
              Exit Sub
          End If
     Next
    
     End Sub '---------->>
    多么想要告诉你 我好喜欢你

  3. #3
    Registered User
    Join Date
    09-04-2014
    Location
    Sydney, Australia
    MS-Off Ver
    2013
    Posts
    52

    Re: Need macro to end once blank is reached

    I have an error coming up at
    If .Range("D" & i).Value <> "" And .Range("E" & i).Value <> "" And .Range("F" & i).Value <> "" Then
    saying compile error: invalid or unqualified reference..
    Last edited by Macro_; 10-02-2014 at 03:06 AM. Reason: spelling error

  4. #4
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Need macro to end once blank is reached

    Edit:

    '==========>>
     Option Explicit
     '---------->>
     Public Sub PassVariables()
           Dim WB As Workbook
           Dim SH As Worksheet
           Set WB = ThisWorkbook
           Set SH = WB.Sheets("Sheet1")
           Dim i As Variant
           For i = 2 To 300 '151
          With SH
           If .Range("D" & i).Value <> "" AND .Range("E" & i).Value <> "" AND .Range("F" & i).Value <> "" Then
           Call Main(myYear:=.Range("A2").Value, _
                     myQuarter:=CStr(.Range("B2").Value), _
                     myFolder:=CStr(.Range("C2").Value), _
                     mySaveAsFolder:=CStr(.Range("D" & i).Value), _
                     mySaveAsName:=CStr(.Range("E" & i).Value), _
                     blCreateFolder:=CStr(.Range("F" & i).Value))
          Else
              Exit Sub
          End If
          End With
     Next
    
     End Sub '---------->>
    Last edited by millz; 10-02-2014 at 05:37 AM.

  5. #5
    Registered User
    Join Date
    09-04-2014
    Location
    Sydney, Australia
    MS-Off Ver
    2013
    Posts
    52

    Re: Need macro to end once blank is reached

    Thank you. wll try it out now

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Change code when a blank cell reached
    By russwongg in forum Excel General
    Replies: 1
    Last Post: 08-26-2014, 09:22 AM
  2. Stopping A Loop When A Blank Cell Is Reached
    By Aaron1978 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-06-2006, 07:48 AM
  3. [SOLVED] Counting blank cells until value is reached
    By Dan in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 03-01-2006, 09:00 AM
  4. Autofill until blank cell is reached
    By uberathlete in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-04-2005, 09:55 PM
  5. Autofill until blank cell is reached
    By uberathlete in forum Excel General
    Replies: 7
    Last Post: 11-04-2005, 01:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1