Hello.
I partly created a code which tries to group data based on the values of an array. The problems are that I'm trying to extract data from multiple worksheets that belong to the same workbook so ReDim Preserve seems to fall out of range when the code reaches the 2nd worksheet. I'm not sure what to do in order to fix it. Change something specific or use another method than ReDim Preserve?
here is the code:
I partly created a code which tries to group data based on the values of an array. The problems are that I'm trying to extract data from multiple worksheets that belong to the same workbook so ReDim Preserve seems to fall out of range when the code reaches the 2nd worksheet. I'm not sure what to do in order to fix it. Change something specific or use another method than ReDim Preserve?
here is the code:
Code:
Sub Scatter_data()
Dim ws As Worksheet, Row As Long, LastRow As Long
Dim BaseValue() As Double, SelectedValue() As Double,
Dim k As Long, n As Long, x As Long, i as Long
Application.ScreenUpdating = False
For Each ws In Worksheets
LastRow = ws.Range("C2").End(xlDown).Row
n = 1
For i = 2 To LastRow
For k = 750 To 3500 Step 250
If ws.Range("J" & i).Value > k - k * 0.01 And ws.Range("J" & i).Value < k + k * 0.01 Then
ReDim Preserve BaseValue(n)
BaseValue(n) = ws.Range("J" & i).Value
n = n + 1
End If
Next k
Next i
ReDim Preserve SelectedValue(UBound(BaseValue), 3)
x = 1
For i = 2 To LastRow
For k = 750 To 3500 Step 250
If ws.Range("J" & i).Value > k - k * 0.01 And ws.Range("J" & i).Value < k + k * 0.01 Then
SelectedValue(x, 1) = ws.Range("J" & i).Value
SelectedValue(x, 2) = ws.Range("N" & i).Value
SelectedValue(x, 3) = ws.Range("AR" & i).Value
x = x + 1
End If
Next k
Next i
Next
Application.ScreenUpdating = True
End Sub