+ Reply to Thread
Results 1 to 7 of 7

Loop Until Empty Cell

Hybrid View

  1. #1
    Registered User
    Join Date
    05-02-2012
    Location
    bc
    MS-Off Ver
    Excel 2007
    Posts
    21

    Loop Until Empty Cell

    Hello,

    Ended up with some compouding issues on my last post and decided to delete and split into two posts.

    Have a spreadsheet with student performance and a macro to copy each student's data from the master sheet into a new tab.

    The macro now creates a tab for each cell in the range, as some co workers have smaller classes, would like to modify the macro to stop when it hits an empty cell. Tried Do until isempty but was not able to make that work. Any suggestions would be great. Thanks.

    the macro
    Sub PagesByDescription()
        Dim rRange As Range, rCell As Range
        Dim wSheet As Worksheet
        Dim wSheetStart As Worksheet
        Dim strText As String
         
        Set wSheetStart = ActiveSheet
        wSheetStart.AutoFilterMode = False
         'Set a range variable to the correct item column
        Set rRange = Range("A9:A50")
         
        On Error Resume Next
        With wSheetStart
            For Each rCell In rRange
                strText = rCell
                .Range("A9").AutoFilter 1, strText
                Worksheets(strText).Delete
                 'Add a sheet named as content of rCell
                Worksheets.Add().Name = strText
                 'Copy the visible filtered range _
                (default of Copy Method) And leave hidden rows
                .UsedRange.Copy Destination:=ActiveSheet.Range("A1")
                ActiveSheet.Cells.Columns.AutoFit
            Next rCell
        End With
         
        With wSheetStart
            .AutoFilterMode = False
            .Activate
        End With
         
        On Error GoTo 0
        Application.DisplayAlerts = True
    
    
    End Sub
    Test1.xlsm

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Loop Until Empty Cell

    try changing your range "rRange" to this:
         'Set a range variable to the correct item column
    '    Set rRange = Range("A9:A50")
        Set rRange = Sheets("All Students").Range("A9")
        Set rRange = Range(rRange, rRange.End(xlDown))
    This will limits your range to the number of students in column A.
    Regards
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

  3. #3
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Loop Until Empty Cell

    For Each rCell In rRange
                strText = rCell
                 If strText = "" then exit For
                .Range("A9").AutoFilter 1, strText
                Worksheets(strText).Delete
                 'Add a sheet named as content of rCell
                Worksheets.Add().Name = strText
                 'Copy the visible filtered range _
                (default of Copy Method) And leave hidden rows
                .UsedRange.Copy Destination:=ActiveSheet.Range("A1")
                ActiveSheet.Cells.Columns.AutoFit
            Next rCell
    Gary's Student

  4. #4
    Registered User
    Join Date
    05-02-2012
    Location
    bc
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Loop Until Empty Cell

    That is great, thanks a lot!

    One thing that I've noticed is that the formula for group averages along the row 5 is inserted as a value in students 2 and beyond but a formula for student 1 - the result is that student 1's group averages equal his score. Is there something I can adjust there so that that doesn't happen?

    Thanks!

  5. #5
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Loop Until Empty Cell

    do the following change and it should copy the formulas:
                 'Copy the visible filtered range _
                (default of Copy Method) And leave hidden rows
                .UsedRange.Copy
                ActiveSheet.Range("A1").PasteSpecial
    Using "pastespecial" will keep your formula.

  6. #6
    Registered User
    Join Date
    05-02-2012
    Location
    bc
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Loop Until Empty Cell

    Thanks, applied to all the cells as desired but am looking to get just the values from the cells and not the formula - is there a modification to this that will paste values from the "all students" page only so that I only have one tab that requires formulas?

    Thanks!!

  7. #7
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Loop Until Empty Cell

    change the paste special line with the following:
                Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
                    xlNone, SkipBlanks:=False, Transpose:=False
                Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
                    SkipBlanks:=False, Transpose:=False
    You will actually copy the data twice. First you copy only values and number format then you copy cells format to get the same look.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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