I am trying to debug the following function, when used as in the sub main listed right above it.
For clarification on the purpose of the arguments for the function it will use 15 is the Interval and 4 is where it will start the range row, 64 is where it will end the range row, 3 is the column the range starts in 3 is the column the range ends in.
The problem lies with the first argument xInterval, while it selects this interval I am getting a single extra selection right before the second. For example Running this code selects (C4, C18, C19, C34, C49, C64) I'm not sure where the C18 is coming from and do not want it. Any suggestions welcome. Thank you!
Sub Main()
Dim rng As Range
Set rng = Every_Fiffteen_min(15, 4, 64, 3, 3)
End Sub
Function Every_Fiffteen_min(xInterval As Integer, row_start As Long, row_end As Long, Colu_start As Integer, Colu_end As Integer) As Range
' Input an interval defined by xInterval that will set the interval of rows to select.
' Using an input for the range row start, row end, column start and column end in numeric values not alpha.
' I have additional functions that will be used to calculate the row start, end, column start and column end, which is why I need these.
Dim CustomRange As Range 'Inputrng
Dim SelectRow As Range ' OutRng
Dim rng As Range
Dim i As Integer
With Sheets("Data Import")
Set CustomRange = .Range(Cells(row_start, Colu_start), Cells(row_end, Colu_end))
Set SelectRow = CustomRange.Rows(xInterval)
For i = 1 To CustomRange.Rows.Count Step xInterval
Set rng = CustomRange.Cells(i, 1)
If SelectRow Is Nothing Then
Set SelectRow = rng
Else
Set SelectRow = Union(SelectRow, CustomRange.Rows(i))
End If
Next i
Application.Goto SelectRow
End With
End Function
CodePudding user response:
The extra range is coming because of your line Set SelectRow = CustomRange.Rows(xInterval)
You already loop from the row_start
to row_end
. So all of the specified Ranges will be added to SelectRow
. But then you also have that line on top of the loop which is adding the unwanted range into SelectRow
.
If you remove this line, the issue will be fixed. But then during the first loop you will be doing Set SelectRow = rng
instead of Set SelectRow = Union(SelectRow, CustomRange.Rows(i))
. This will again change the output range, which doesn't seem to fit your desired output based on the description you gave.
To make everything fixed, also change the line Set rng = CustomRange.Cells(i, 1)
to be Set rng = CustomRange.Rows(i)