Hi all,
I searched the board and couldn't quite find anything that addresses this so I apologize if it is already solved.
I am using a script posted on Microsoft's website to programmatically select every nth row in a range in Excel here. I have posted the code below:
For the most part, it works very well. The problem occurs when I try to run the script when I have selected any number of rows starting below row 1 - then only the first third row is selected, regardless of the size of the range.
I have looked at the code, but can't see where the problem is. Can anyone find it? Thanks!
Ryan
***CODE START***
Sub SelectEveryNthRow()
' Initialize ColsSelection equal to the number of columns in the
' selection.
ColsSelection = Selection.Columns.Count
' Initialize RowsSelection equal to the number of rows in your
' selection.
RowsSelection = Selection.Rows.Count
' Initialize RowsBetween equal to three.
RowsBetween = 3
' Initialize Diff equal to one row less than the first row number of
' the selection.
Diff = Selection.Row - 1
' Resize the selection to be 1 column wide and the same number of
' rows long as the initial selection.
Selection.Resize(RowsSelection, 1).Select
' Resize the selection to be every third row and the same number of
' columns wide as the original selection.
Set FinalRange = Selection. _
Offset(RowsBetween - 1, 0).Resize(1,ColsSelection)
' Loop through each cell in the selection.
For Each xCell In Selection
' If the row number is a multiple of 3, then . . .
If xCell.Row Mod RowsBetween = Diff Then
' ...reset FinalRange to include the union of the current
' FinalRange and the same number of columns.
Set FinalRange = Application.Union _
(FinalRange, xCell.Resize(1,ColsSelection))
' End check.
End If
' Iterate loop.
Next xCell
' Select the requested cells in the range.
FinalRange.Select
End Sub
***CODE END***
I searched the board and couldn't quite find anything that addresses this so I apologize if it is already solved.
I am using a script posted on Microsoft's website to programmatically select every nth row in a range in Excel here. I have posted the code below:
For the most part, it works very well. The problem occurs when I try to run the script when I have selected any number of rows starting below row 1 - then only the first third row is selected, regardless of the size of the range.
I have looked at the code, but can't see where the problem is. Can anyone find it? Thanks!
Ryan
***CODE START***
Sub SelectEveryNthRow()
' Initialize ColsSelection equal to the number of columns in the
' selection.
ColsSelection = Selection.Columns.Count
' Initialize RowsSelection equal to the number of rows in your
' selection.
RowsSelection = Selection.Rows.Count
' Initialize RowsBetween equal to three.
RowsBetween = 3
' Initialize Diff equal to one row less than the first row number of
' the selection.
Diff = Selection.Row - 1
' Resize the selection to be 1 column wide and the same number of
' rows long as the initial selection.
Selection.Resize(RowsSelection, 1).Select
' Resize the selection to be every third row and the same number of
' columns wide as the original selection.
Set FinalRange = Selection. _
Offset(RowsBetween - 1, 0).Resize(1,ColsSelection)
' Loop through each cell in the selection.
For Each xCell In Selection
' If the row number is a multiple of 3, then . . .
If xCell.Row Mod RowsBetween = Diff Then
' ...reset FinalRange to include the union of the current
' FinalRange and the same number of columns.
Set FinalRange = Application.Union _
(FinalRange, xCell.Resize(1,ColsSelection))
' End check.
End If
' Iterate loop.
Next xCell
' Select the requested cells in the range.
FinalRange.Select
End Sub
***CODE END***