Subscript out of range error

graemeal

Active Member
Joined
May 17, 2007
Messages
316
Platform
  1. Windows
The first code is my original that works fine. Activated by CTL F

I want to remove one of the ranges and change the other one within the code EG

When I try to activate the bottom code by CTL F I get a Subscript out of range error

Thanks

From this

Code:
Range("J2:J1839").Formula = "=IF(AND(G2,D2>1000,C2>1000),D2/C2,0)"
Range("L2:L1839").Formula = "=IF(AND(G2,D2>1000,C2>1000),Y2/Z2,0)"

To

Code:
Range("J2:J1839").Formula = "=IF(AND(G2=0,J2>1.1,L2>1.2,D2>1000,C2>1000),J2+L2/2,0)"


Code:
Sub Macro5()
Windows("1876.xlsm").Activate
    Sheets("Sheet1").Select
    Range("J2:J1839").Formula = "=IF(AND(G2,D2>1000,C2>1000),D2/C2,0)"
    Range("L2:L1839").Formula = "=IF(AND(G2,D2>1000,C2>1000),Y2/Z2,0)"
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "0"
    Range("A2").Select
    Selection.AutoFill Destination:=Range("A2:A1839")
    Range("A2:A1839").Select
    Range("A2").Select
End Sub


To This

Code:
Sub Macro5()
Windows("1876.xlsm").Activate
    Sheets("Sheet1").Select
    Range("J2:J1839").Formula = "=IF(AND(G2=0,J2>1.1,L2>1.2,D2>1000,C2>1000),J2+L2/2,0)"
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "0"
    Range("A2").Select
    Selection.AutoFill Destination:=Range("A2:A1839")
    Range("A2:A1839").Select
    Range("A2").Select
End Sub
 
The code in Post #10 works fine for me !!

Is the workbook you are referencing open ??
Code:
"Windows("1876.xlsm").Activate"
I think it should be
Code:
Workbooks("1876.xlsm").Activate

If it isn't open you will have to reference the file and path
 
Last edited:
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
The code should have Sub and End Sub, which I assumed you had.

Goto Insert>Module and add the code in the newly created module.
Code:
Sub Test()

Dim wb As Workbook
Dim ws As Worksheet

    Set wb = ThisWorkbook
 
    Set ws = wb.Sheets("Sheet1")
 
    With ws
        .Range("K2:K1839").Formula = "=IF(AND(G2=0,J2>1.1,L2>1.2,D2>1000,C2>1000),J2+L2/2,0)"
        .Range("A2").Value = "0"
        .Range("A2").AutoFill Destination:=Range("A2:A1839")
    End With

End Sub
 
Upvote 0
Oh dear!!! I had saved the file name to 1877 so as not to mess up my original sheet and forgot about the
Code:
Windows("1876.xlsm").Activate

So sorry about all this

Apoligies again

Thank so much
 
Upvote 0
Ahh, glad you got it sorted....another victim of the Seniors Moment...LOL !!
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,795
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top