0

I am completely new to programming here.

I am getting a subscript out of range error on the following code:

b(i) = ThisWorkbook.Sheets("trades").Range("AA & arxi +i-1:AD& arxi+i-1").Value

I believe it has to do with the syntax, or maybe because I haven't redimed b(vdomada,4) ?

In any case the data from the range do not go into the array. The result would be to input the data in the array and then export them in a different excel file(the exporting bit is not into the code provided)

Thanks in advance for your help.

Sub report()
Dim b As Variant, arxi As Integer, telos As Variant
Dim LastDayRow As Integer, vdomada As Integer, i As Integer, z As Integer
Dim LastDay As Date


Application.Workbooks("Back Office 2019.xlsx").Activate

Sheets("trades").Select
LastDayRow = Range("L" & Rows.Count).End(xlUp).Row
telos = Cells(LastDayRow, 12)
arxi = InputBox("Please enter the row for the start of the week")
vdomada = LastDayRow - arxi
ReDim b(vdomada) As Variant
For i = 1 To vdomada

b(i) = ThisWorkbook.Sheets("trades").Range("AA & arxi +i-1:AD& arxi+i-1").Value
Next i

End Sub
2
  • Change the line to this b(i) = ThisWorkbook.Sheets("UST trades").Range("AA" & arxi + i - 1 & ":AD" & arxi + i - 1).Value Commented Jun 17, 2019 at 15:22
  • Try to avoid activate and select. Commented Jun 17, 2019 at 15:49

3 Answers 3

2

Few things that might help. I would remove the following-

ReDim b(vdomada) As Variant
For i = 1 To vdomada

b(i) = ThisWorkbook.Sheets("trades").Range("AA & arxi +i-1:AD& arxi+i-1").Value
Next i

And replace it with-

b = ThisWorkbook.Sheets("trades").Range("AA" & arxi & ":AD" & vdomada).Value

See if that helps.

I think your error originates from giving Range something it doesn't know what to do with and from trying to assign a 2D range to an array that's been defined as 1D. I don't think you need to ReDim the array in this case.

Good luck.

Sign up to request clarification or add additional context in comments.

Comments

1
With ThisWorkbook.Sheets("UST trades")
    b(i) = .Range(.Cells(arxi + i - 1, "AA"), .Cells(arxi + i - 1, "AD")).Value
End With

Comments

0

How to avoid .Activate

With Workbooks("Back Office 2019.xlsx").Sheets("trades")
    LastDayRow = .Range("L" & Rows.Count).End(xlUp).Row
    telos = .Cells(LastDayRow, 12)
    arxi = InputBox("Please enter the row for the start of the week")
    vdomada = LastDayRow - arxi
End With

Additionally, variables which takes whole numbers( NOT decimals) is better to declare As Long. Longs can take higher bigger numbers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.