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!










share|improve this question
























  • Error on which line specifically?
    – Kubie
    Nov 9 at 3:17















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!










share|improve this question
























  • Error on which line specifically?
    – Kubie
    Nov 9 at 3:17













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!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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.






share|improve this answer























  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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.






share|improve this answer























  • 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

















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.






share|improve this answer























  • 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















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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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




















  • 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




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff