I'm having troubles with my code. I'm trying to get data posted from one worksheet to another. I defined two worksheets in my code, and put my sheet range in an array. Then I wanted to create a loop that would post the data from column E from the business sheet in my engagment sheet, if column D has a number of 3 or higher. However, when I run it it says
Subscript is out if range
My code is as followed
Sub Simple_if2()
Dim shB As Worksheet, shE As Worksheet, lastRB As Long, lastRE As Long
Dim score As Integer, i As Long, k As Long, arrB As Variant
'Worksheet definitions
Set shB = Worksheets("Business")
Set shE = Worksheets("Engagement Plan (High Priority)")
lastRB = shB.Range("D" & shB.Rows.Count).End(xlUp).Row
lastRE = shB.Range("E" & shB.Rows.Count).End(xlUp).Row
arrB = shB.Range("D6:E6" & lastRB & lastRE).Value 'put the shB sheet range in an array
ReDim arrE(UBound(arrB)) 'redim arrE at maximum possible dimension
'Loop for Business worksheet
For i = 1 To UBound(arrB)
If arrB(2, i) >= 3 Then
arrE(k) = arrB(1, i) 'fill arrE only with elements >=3
k = k + 1
End If
Next i
ReDim Preserve arrE(k + 1) 'redim the array to keep only the filled elements
'drop the array content at once:
shE.Range("B3").Resize(UBound(arrE) + 1, 1).Value = WorksheetFunction.Transpose(arrE)
End Sub
shB.Range("D6:E6" & lastRB & lastRE)
is probably the wrong range...Debug.Print "D6:E6" & lastRB & lastRE
and see what the result is. – BigBenarrB(i, 2)
andarrB(i, 1)
– Scott CranerDebug.Print "D6:E6" & lastRB & lastRE
. The result is going to be something likeD6:E65555
in the Immediate Window. – BigBenarrB=shB.Range("D6:E" & lastRB).value
worked. Thanks man – Anders Vigen