VBA - Subscript Out of Range Error on MOD command
up vote
1
down vote
favorite
I am still a novice VBA user. I can't seem to get my head around why this bit of script is not working. I get a
'Subscript Out of Range' error
on the second line in the If
statement of the below:
Sub ScreenUpdate()
Dim LastRow As Long, LastColumn As Long, i As Long, j As Long
Application.ScreenUpdating = False
LastRow = Range("C" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Cells(i, 17).Value = Cells(i - 1, 17).Value Then
Cells(i, 1).NumberFormat = ";;;"
Cells(i, 2).NumberFormat = ";;;"
Cells(i, 15).NumberFormat = ";;;"
Cells(i, 16).NumberFormat = ";;;"
Else
Cells(i, 1).NumberFormat = "0"
Cells(i, 2).NumberFormat = "0"
Cells(i, 15).NumberFormat = "0"
Cells(i, 16).NumberFormat = "0"
End If
Next i
For j = 2 To LastRow
If Cells(j, 17).Value Mod 2 = 1 Then
Range(Cells(j, 1), Cells(j, 16)).Interior.ColorIndex = RGB(221, 235, 247)
Else
Range(Cells(j, 1), Cells(j, 16)).Interior.ColorIndex = RGB(255, 255, 255)
End If
Next j
Application.ScreenUpdating = True
End Sub
Many thanks for your assistance!
excel vba excel-vba
add a comment |
up vote
1
down vote
favorite
I am still a novice VBA user. I can't seem to get my head around why this bit of script is not working. I get a
'Subscript Out of Range' error
on the second line in the If
statement of the below:
Sub ScreenUpdate()
Dim LastRow As Long, LastColumn As Long, i As Long, j As Long
Application.ScreenUpdating = False
LastRow = Range("C" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Cells(i, 17).Value = Cells(i - 1, 17).Value Then
Cells(i, 1).NumberFormat = ";;;"
Cells(i, 2).NumberFormat = ";;;"
Cells(i, 15).NumberFormat = ";;;"
Cells(i, 16).NumberFormat = ";;;"
Else
Cells(i, 1).NumberFormat = "0"
Cells(i, 2).NumberFormat = "0"
Cells(i, 15).NumberFormat = "0"
Cells(i, 16).NumberFormat = "0"
End If
Next i
For j = 2 To LastRow
If Cells(j, 17).Value Mod 2 = 1 Then
Range(Cells(j, 1), Cells(j, 16)).Interior.ColorIndex = RGB(221, 235, 247)
Else
Range(Cells(j, 1), Cells(j, 16)).Interior.ColorIndex = RGB(255, 255, 255)
End If
Next j
Application.ScreenUpdating = True
End Sub
Many thanks for your assistance!
excel vba excel-vba
Error on which line specifically?
– Kubie
Nov 9 at 3:17
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am still a novice VBA user. I can't seem to get my head around why this bit of script is not working. I get a
'Subscript Out of Range' error
on the second line in the If
statement of the below:
Sub ScreenUpdate()
Dim LastRow As Long, LastColumn As Long, i As Long, j As Long
Application.ScreenUpdating = False
LastRow = Range("C" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Cells(i, 17).Value = Cells(i - 1, 17).Value Then
Cells(i, 1).NumberFormat = ";;;"
Cells(i, 2).NumberFormat = ";;;"
Cells(i, 15).NumberFormat = ";;;"
Cells(i, 16).NumberFormat = ";;;"
Else
Cells(i, 1).NumberFormat = "0"
Cells(i, 2).NumberFormat = "0"
Cells(i, 15).NumberFormat = "0"
Cells(i, 16).NumberFormat = "0"
End If
Next i
For j = 2 To LastRow
If Cells(j, 17).Value Mod 2 = 1 Then
Range(Cells(j, 1), Cells(j, 16)).Interior.ColorIndex = RGB(221, 235, 247)
Else
Range(Cells(j, 1), Cells(j, 16)).Interior.ColorIndex = RGB(255, 255, 255)
End If
Next j
Application.ScreenUpdating = True
End Sub
Many thanks for your assistance!
excel vba excel-vba
I am still a novice VBA user. I can't seem to get my head around why this bit of script is not working. I get a
'Subscript Out of Range' error
on the second line in the If
statement of the below:
Sub ScreenUpdate()
Dim LastRow As Long, LastColumn As Long, i As Long, j As Long
Application.ScreenUpdating = False
LastRow = Range("C" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Cells(i, 17).Value = Cells(i - 1, 17).Value Then
Cells(i, 1).NumberFormat = ";;;"
Cells(i, 2).NumberFormat = ";;;"
Cells(i, 15).NumberFormat = ";;;"
Cells(i, 16).NumberFormat = ";;;"
Else
Cells(i, 1).NumberFormat = "0"
Cells(i, 2).NumberFormat = "0"
Cells(i, 15).NumberFormat = "0"
Cells(i, 16).NumberFormat = "0"
End If
Next i
For j = 2 To LastRow
If Cells(j, 17).Value Mod 2 = 1 Then
Range(Cells(j, 1), Cells(j, 16)).Interior.ColorIndex = RGB(221, 235, 247)
Else
Range(Cells(j, 1), Cells(j, 16)).Interior.ColorIndex = RGB(255, 255, 255)
End If
Next j
Application.ScreenUpdating = True
End Sub
Many thanks for your assistance!
excel vba excel-vba
excel vba excel-vba
edited Nov 9 at 2:22
BigBen
4,5652417
4,5652417
asked Nov 9 at 2:16
DushPops
61
61
Error on which line specifically?
– Kubie
Nov 9 at 3:17
add a comment |
Error on which line specifically?
– Kubie
Nov 9 at 3:17
Error on which line specifically?
– Kubie
Nov 9 at 3:17
Error on which line specifically?
– Kubie
Nov 9 at 3:17
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You've mixed up Color
, which you would use the RGB
function with, and ColorIndex
.
The RGB
function returns a Long
whole number corresponding to the color value. Specifically in this case,
RGB(221, 235, 247)
returns 16,247,773
RGB(255, 255, 255)
returns 16,777,215
Both of these are outside the very narrow range of possible values for a color index in the current color palette, hence the subscript out of range
.
Change each instance of ColorIndex
to Color
.
Thanks so much BigBen! Yep that was a result of me copying some script that worked and then thinking I could edit it to do something else slightly different! Thank you very much indeed!
– DushPops
Nov 11 at 6:24
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You've mixed up Color
, which you would use the RGB
function with, and ColorIndex
.
The RGB
function returns a Long
whole number corresponding to the color value. Specifically in this case,
RGB(221, 235, 247)
returns 16,247,773
RGB(255, 255, 255)
returns 16,777,215
Both of these are outside the very narrow range of possible values for a color index in the current color palette, hence the subscript out of range
.
Change each instance of ColorIndex
to Color
.
Thanks so much BigBen! Yep that was a result of me copying some script that worked and then thinking I could edit it to do something else slightly different! Thank you very much indeed!
– DushPops
Nov 11 at 6:24
add a comment |
up vote
1
down vote
You've mixed up Color
, which you would use the RGB
function with, and ColorIndex
.
The RGB
function returns a Long
whole number corresponding to the color value. Specifically in this case,
RGB(221, 235, 247)
returns 16,247,773
RGB(255, 255, 255)
returns 16,777,215
Both of these are outside the very narrow range of possible values for a color index in the current color palette, hence the subscript out of range
.
Change each instance of ColorIndex
to Color
.
Thanks so much BigBen! Yep that was a result of me copying some script that worked and then thinking I could edit it to do something else slightly different! Thank you very much indeed!
– DushPops
Nov 11 at 6:24
add a comment |
up vote
1
down vote
up vote
1
down vote
You've mixed up Color
, which you would use the RGB
function with, and ColorIndex
.
The RGB
function returns a Long
whole number corresponding to the color value. Specifically in this case,
RGB(221, 235, 247)
returns 16,247,773
RGB(255, 255, 255)
returns 16,777,215
Both of these are outside the very narrow range of possible values for a color index in the current color palette, hence the subscript out of range
.
Change each instance of ColorIndex
to Color
.
You've mixed up Color
, which you would use the RGB
function with, and ColorIndex
.
The RGB
function returns a Long
whole number corresponding to the color value. Specifically in this case,
RGB(221, 235, 247)
returns 16,247,773
RGB(255, 255, 255)
returns 16,777,215
Both of these are outside the very narrow range of possible values for a color index in the current color palette, hence the subscript out of range
.
Change each instance of ColorIndex
to Color
.
edited Nov 9 at 3:28
answered Nov 9 at 3:22
BigBen
4,5652417
4,5652417
Thanks so much BigBen! Yep that was a result of me copying some script that worked and then thinking I could edit it to do something else slightly different! Thank you very much indeed!
– DushPops
Nov 11 at 6:24
add a comment |
Thanks so much BigBen! Yep that was a result of me copying some script that worked and then thinking I could edit it to do something else slightly different! Thank you very much indeed!
– DushPops
Nov 11 at 6:24
Thanks so much BigBen! Yep that was a result of me copying some script that worked and then thinking I could edit it to do something else slightly different! Thank you very much indeed!
– DushPops
Nov 11 at 6:24
Thanks so much BigBen! Yep that was a result of me copying some script that worked and then thinking I could edit it to do something else slightly different! Thank you very much indeed!
– DushPops
Nov 11 at 6:24
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53218910%2fvba-subscript-out-of-range-error-on-mod-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Error on which line specifically?
– Kubie
Nov 9 at 3:17