+ Reply to Thread
Results 1 to 18 of 18

VBA Sorting Nth Range not working

Hybrid View

  1. #1
    Registered User
    Join Date
    01-15-2006
    Posts
    46

    VBA Sorting Nth Range not working

    I have a values in column C of stuff1, stuff2 and on down.

    I have a value in cell A1.

    I want to start in C1, and every Nth, where N is the value in cell A1, select and paste into column H.

    My problem is that the selected cells don't start in C1, in fact, based on the value in A1, it starts in different cells in column C. AND, if i press the button twice, the returned values are different.

    Private Sub CommandButton1_Click()
    'Sub SelectEveryNthCell()
        Dim rRange As Range
        Dim rEveryNth As Range
        Dim lRow As Long
         
        With Sheet1
            Set rRange = .Range("C1", .Cells(.Rows.Count, "C").End(xlUp))
        End With
         
        For lRow = 1 To rRange.Rows.Count Step Range("a1").Value
        
            If lRow = 1 Then
                Set rEveryNth = rRange(lRow, 1)
            Else
                Set rEveryNth = Union(rRange(lRow, 1), rEveryNth)
            End If
        Next lRow
         
        Application.Goto rEveryNth
        
        Selection.Copy
        
        ActiveSheet.Range("j1").Select
        
        Selection.PasteSpecial Paste:=xlPasteValues, _
        Operation:=xlNone, _
        SkipBlanks:=False, _
        Transpose:=False
    
    
    End Sub
    Link to source code
    i've attached a sample.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    07-17-2005
    Location
    Abergavenny, Wales, UK
    MS-Off Ver
    XL2003, XL2007, XL2010, XL2013, XL2016
    Posts
    608

    Re: VBA Sorting Nth Range not working

    Hi

    The code runs fine for me.
    I would amend it slightly to switch off screen updating, and to clear the contents of column J before copying the new data range to J1.
    Amended code shown below
    Private Sub CommandButton1_Click()
    'Sub SelectEveryNthCell()
        Dim rRange As Range
        Dim rEveryNth As Range
        Dim lRow As Long
        Application.ScreenUpdating = False
        With Sheet1
            Set rRange = .Range("C1", .Cells(.Rows.Count, "C").End(xlUp))
        End With
    
        For lRow = 1 To rRange.Rows.Count Step Range("a1").Value
    
            If lRow = 1 Then
                Set rEveryNth = rRange(lRow, 1)
            Else
                Set rEveryNth = Union(rRange(lRow, 1), rEveryNth)
            End If
        Next lRow
        ActiveSheet.Columns("J:J").ClearContents
        Application.Goto rEveryNth
    
        Selection.Copy
    
        ActiveSheet.Range("j1").Select
    
        Selection.PasteSpecial Paste:=xlPasteValues, _
                               Operation:=xlNone, _
                               SkipBlanks:=False, _
                               Transpose:=False
    
        ActiveSheet.Range("A1").Activate
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
        'http://www.ozgrid.com/forum/showthread.php?p=111223
        'http://www.ozgrid.com/forum/showthread.php?t=91759
      
    End Sub
    Last edited by Leith Ross; 05-16-2009 at 05:43 PM. Reason: Added Code Tags
    --
    Regards
    Roger Govier
    Microsoft Excel MVP

  3. #3
    Registered User
    Join Date
    01-15-2006
    Posts
    46

    Re: VBA Sorting Nth Range not working

    Thanks Roger,

    I'm still getting the same results:

    i.e.

    A1= 1 works
    A1= 2 starts at "stuff99" and every 2 on down
    A1= 3 starts at "stuff79" and every 3 on down
    A1= 4 starts at "stuff53" and every 4 on down
    A1= 5 starts at "stuff31" and every 5 on down
    A1= 6 starts at "stuff7" and every 6 on down
    A1= 7 works

    A1= 8 and more, the code works fine for me. I've yet to try it on another machine, but i must use this code on this machine, so i have to figure this out.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VBA Sorting Nth Range not working

    Roger,

    Please edit your post to add code tags.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    01-15-2006
    Posts
    46

    Re: VBA Sorting Nth Range not working

    the final result, is that i want to have the value in A1 be the # of personnel reporting, and divy up the part to be made based on that value into each persons list of work, so i have to start with C1 as the first part to be made, not part 99 or 73 etc.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA Sorting Nth Range not working

    Hello Beechum1,

    This macro has been added to the attached workbook.
    Private Sub CommandButton1_Click()
    
    'Sub SelectEveryNthCell()
      Dim Cell As Range
      Dim R As Long
      Dim rRange As Range
      Dim rEveryNth As Range
      Dim lRow As Long
        
        R = 1
        
        With Sheet1
            Set rRange = .Range("C1", .Cells(.Rows.Count, "C").End(xlUp))
        End With
         
        For lRow = 1 To rRange.Rows.Count Step Range("a1").Value
            If lRow = 1 Then
                Set rEveryNth = rRange(lRow, 1)
            Else
                Set rEveryNth = Union(rEveryNth, rRange(lRow, 1))
            End If
        Next lRow
        
        For Each Cell In rEveryNth
          Sheets("Sheet1").Cells(R, "J") = Cell
          R = R + 1
        Next Cell
        
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Registered User
    Join Date
    01-15-2006
    Posts
    46

    Re: VBA Sorting Nth Range not working

    So this is what i have figured out so far, but i need to assign some "ifs" based on the value in A1...

    Private Sub CommandButton1_Click()
    
    'Sub SelectEveryNthCell()
      Dim Cell As Range
      Dim R As Long
      Dim rRange As Range
      Dim rEveryNth As Range
      Dim lRow As Long
      
      Call Person_1
      Call Person_2
      
      End Sub

  8. #8
    Registered User
    Join Date
    01-15-2006
    Posts
    46

    Re: VBA Sorting Nth Range not working

    I found this Here


    Private Sub Worksheet_Change(ByVal Target As Excel.Range) 
        If Target.Address = "$A$1" Then 
            Select  Case Target.Value 
            Case 5 To 10: MyMacro (1) 
            Case Is > 10: MyMacro (2) 
            Case Else ' do nothing
            End Select 
        End If 
    End Sub
    But i can't figure out how this applies to what i'm doing exactly. I'm sure that i'll have to change the Case statements to:

    Case 1
      Call Person_1
    
    Case 2
    
      Call Person_1
      Call Person_2
    
    Case else do nothing

+ 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