2D For Loop used on a Staff Leave Calendar










0















this is the first time I have ever posted in a forum so please forgive me if I slip up with protocols and be a little patient with me.



I am completely self taught where coding is concerned and have always managed to find my answers from other peoples posts in the past. This current problem is vexing me though because I just do not understand enough about VBA to see the solution. The code as it stands now spits out a
"Run-time error '1004':
Application-defined or object-defined error"
I have tried researching this error also and found many answers on the topic but am not sure how to apply them to my code. I am fairly sure I will need to add a "With" in there but I would like some professional help with it before I mess with the code too much more.



The purpose behind my code is to match names on a Calendar in Sheet2 (Current Staff list) to a growing list of Names where staff are requesting leave in Sheet1. Where there is a match I want to check the row on Sheet2 which contains calendar dates whether it is >= a leave start date AND <= a leave end date. Then highlight the cells where this is true.
Then it needs to continue checking the same row on Sheet2 against the list of names on Sheet1 to find additional matches and do the same actions.



Sub Highlight_Calendar()

Dim lRow1 As Long
lRow1 = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row
Dim lRow2 As Long
lRow2 = Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count, 1).End(xlUp).Row
Dim lCol2 As Long
lCol2 = Worksheets("Sheet2").Cells(lRow2, Worksheets("Sheet2").Columns.Count).End(xlToLeft).Column
Dim ArrS2Names() As Variant
ArrS2Names = Sheet2.Range("A3", Worksheets("Sheet2").Cells(lRow2, 1))
Dim ArrS1Names() As Variant
ArrS1Names = Sheet1.Range("A3", Worksheets("Sheet1").Cells(lRow1, 1))
Dim calendarArr() As Variant
calendarArr = Sheet2.Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2))
Dim firstArr() As Variant
firstArr = Sheet1.Range("C3:C" & lRow1)
Dim lastArr() As Variant
lastArr = Sheet1.Range("D3:D" & lRow1)

Dim R1 As Long
Dim R2 As Long
Dim C2 As Long

For R2 = LBound(ArrS2Names, 1) To UBound(ArrS2Names, 1)
For R1 = LBound(ArrS1Names, 1) To UBound(ArrS1Names, 1)
For C2 = LBound(calendarArr, 2) To UBound(calendarArr, 2)
If ArrS2Names(R2, 1) = ArrS1Names(R1, 1) Then
Debug.Print (ArrS2Names(R2, 1))
If calendarArr(R2, C2) >= firstArr(R1, 1) And calendarArr(R2, C2) <= lastArr(R1, 1) Then
Sheet2.Cells(R2, C2).Interior.Color = RGB(0, 255, 0)
Debug.Print (Sheet2.Cells(R2, C2))
End If
End If
Next C2
Next R1
Next R2
End Sub









share|improve this question
























  • This would be easier to visualize with some mock data. Would you be able to edit your question and post a link to relevant screen captures?

    – cybernetic.nomad
    Nov 15 '18 at 22:43











  • Since ArrS2Names is an array, ArrS2Names(R2, "A") will fail. Maybe ArrS2Names(R2, 1) instead?

    – chris neilsen
    Nov 15 '18 at 22:59











  • Thanks I have edited the sheet to contain your suggestions, I am still getting a runtime error though even after changing all of the column letters for their corresponding numbers. I have also added the links (hopefully correctly) so everyone can see the spreadsheets I am working with. When I "debug" the error it highlights line 4 or "lRow1 = Worksheets("Sheet1").Range(Worksheets("Sheet1").Rows.Count, Worksheets("Sheet1").Columns("A")).End(x1Up).Row" not sure if this is significant?

    – S.Hawkes
    Nov 15 '18 at 23:06












  • Where is the code throwing the error?

    – a-burge
    Nov 15 '18 at 23:07











  • Change that line to Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row Note you have x1Up (ie x One Up) should be xlUp (ie x El Up) You will need to make similar changes elsewhere too

    – chris neilsen
    Nov 15 '18 at 23:13
















0















this is the first time I have ever posted in a forum so please forgive me if I slip up with protocols and be a little patient with me.



I am completely self taught where coding is concerned and have always managed to find my answers from other peoples posts in the past. This current problem is vexing me though because I just do not understand enough about VBA to see the solution. The code as it stands now spits out a
"Run-time error '1004':
Application-defined or object-defined error"
I have tried researching this error also and found many answers on the topic but am not sure how to apply them to my code. I am fairly sure I will need to add a "With" in there but I would like some professional help with it before I mess with the code too much more.



The purpose behind my code is to match names on a Calendar in Sheet2 (Current Staff list) to a growing list of Names where staff are requesting leave in Sheet1. Where there is a match I want to check the row on Sheet2 which contains calendar dates whether it is >= a leave start date AND <= a leave end date. Then highlight the cells where this is true.
Then it needs to continue checking the same row on Sheet2 against the list of names on Sheet1 to find additional matches and do the same actions.



Sub Highlight_Calendar()

Dim lRow1 As Long
lRow1 = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row
Dim lRow2 As Long
lRow2 = Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count, 1).End(xlUp).Row
Dim lCol2 As Long
lCol2 = Worksheets("Sheet2").Cells(lRow2, Worksheets("Sheet2").Columns.Count).End(xlToLeft).Column
Dim ArrS2Names() As Variant
ArrS2Names = Sheet2.Range("A3", Worksheets("Sheet2").Cells(lRow2, 1))
Dim ArrS1Names() As Variant
ArrS1Names = Sheet1.Range("A3", Worksheets("Sheet1").Cells(lRow1, 1))
Dim calendarArr() As Variant
calendarArr = Sheet2.Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2))
Dim firstArr() As Variant
firstArr = Sheet1.Range("C3:C" & lRow1)
Dim lastArr() As Variant
lastArr = Sheet1.Range("D3:D" & lRow1)

Dim R1 As Long
Dim R2 As Long
Dim C2 As Long

For R2 = LBound(ArrS2Names, 1) To UBound(ArrS2Names, 1)
For R1 = LBound(ArrS1Names, 1) To UBound(ArrS1Names, 1)
For C2 = LBound(calendarArr, 2) To UBound(calendarArr, 2)
If ArrS2Names(R2, 1) = ArrS1Names(R1, 1) Then
Debug.Print (ArrS2Names(R2, 1))
If calendarArr(R2, C2) >= firstArr(R1, 1) And calendarArr(R2, C2) <= lastArr(R1, 1) Then
Sheet2.Cells(R2, C2).Interior.Color = RGB(0, 255, 0)
Debug.Print (Sheet2.Cells(R2, C2))
End If
End If
Next C2
Next R1
Next R2
End Sub









share|improve this question
























  • This would be easier to visualize with some mock data. Would you be able to edit your question and post a link to relevant screen captures?

    – cybernetic.nomad
    Nov 15 '18 at 22:43











  • Since ArrS2Names is an array, ArrS2Names(R2, "A") will fail. Maybe ArrS2Names(R2, 1) instead?

    – chris neilsen
    Nov 15 '18 at 22:59











  • Thanks I have edited the sheet to contain your suggestions, I am still getting a runtime error though even after changing all of the column letters for their corresponding numbers. I have also added the links (hopefully correctly) so everyone can see the spreadsheets I am working with. When I "debug" the error it highlights line 4 or "lRow1 = Worksheets("Sheet1").Range(Worksheets("Sheet1").Rows.Count, Worksheets("Sheet1").Columns("A")).End(x1Up).Row" not sure if this is significant?

    – S.Hawkes
    Nov 15 '18 at 23:06












  • Where is the code throwing the error?

    – a-burge
    Nov 15 '18 at 23:07











  • Change that line to Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row Note you have x1Up (ie x One Up) should be xlUp (ie x El Up) You will need to make similar changes elsewhere too

    – chris neilsen
    Nov 15 '18 at 23:13














0












0








0


0






this is the first time I have ever posted in a forum so please forgive me if I slip up with protocols and be a little patient with me.



I am completely self taught where coding is concerned and have always managed to find my answers from other peoples posts in the past. This current problem is vexing me though because I just do not understand enough about VBA to see the solution. The code as it stands now spits out a
"Run-time error '1004':
Application-defined or object-defined error"
I have tried researching this error also and found many answers on the topic but am not sure how to apply them to my code. I am fairly sure I will need to add a "With" in there but I would like some professional help with it before I mess with the code too much more.



The purpose behind my code is to match names on a Calendar in Sheet2 (Current Staff list) to a growing list of Names where staff are requesting leave in Sheet1. Where there is a match I want to check the row on Sheet2 which contains calendar dates whether it is >= a leave start date AND <= a leave end date. Then highlight the cells where this is true.
Then it needs to continue checking the same row on Sheet2 against the list of names on Sheet1 to find additional matches and do the same actions.



Sub Highlight_Calendar()

Dim lRow1 As Long
lRow1 = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row
Dim lRow2 As Long
lRow2 = Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count, 1).End(xlUp).Row
Dim lCol2 As Long
lCol2 = Worksheets("Sheet2").Cells(lRow2, Worksheets("Sheet2").Columns.Count).End(xlToLeft).Column
Dim ArrS2Names() As Variant
ArrS2Names = Sheet2.Range("A3", Worksheets("Sheet2").Cells(lRow2, 1))
Dim ArrS1Names() As Variant
ArrS1Names = Sheet1.Range("A3", Worksheets("Sheet1").Cells(lRow1, 1))
Dim calendarArr() As Variant
calendarArr = Sheet2.Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2))
Dim firstArr() As Variant
firstArr = Sheet1.Range("C3:C" & lRow1)
Dim lastArr() As Variant
lastArr = Sheet1.Range("D3:D" & lRow1)

Dim R1 As Long
Dim R2 As Long
Dim C2 As Long

For R2 = LBound(ArrS2Names, 1) To UBound(ArrS2Names, 1)
For R1 = LBound(ArrS1Names, 1) To UBound(ArrS1Names, 1)
For C2 = LBound(calendarArr, 2) To UBound(calendarArr, 2)
If ArrS2Names(R2, 1) = ArrS1Names(R1, 1) Then
Debug.Print (ArrS2Names(R2, 1))
If calendarArr(R2, C2) >= firstArr(R1, 1) And calendarArr(R2, C2) <= lastArr(R1, 1) Then
Sheet2.Cells(R2, C2).Interior.Color = RGB(0, 255, 0)
Debug.Print (Sheet2.Cells(R2, C2))
End If
End If
Next C2
Next R1
Next R2
End Sub









share|improve this question
















this is the first time I have ever posted in a forum so please forgive me if I slip up with protocols and be a little patient with me.



I am completely self taught where coding is concerned and have always managed to find my answers from other peoples posts in the past. This current problem is vexing me though because I just do not understand enough about VBA to see the solution. The code as it stands now spits out a
"Run-time error '1004':
Application-defined or object-defined error"
I have tried researching this error also and found many answers on the topic but am not sure how to apply them to my code. I am fairly sure I will need to add a "With" in there but I would like some professional help with it before I mess with the code too much more.



The purpose behind my code is to match names on a Calendar in Sheet2 (Current Staff list) to a growing list of Names where staff are requesting leave in Sheet1. Where there is a match I want to check the row on Sheet2 which contains calendar dates whether it is >= a leave start date AND <= a leave end date. Then highlight the cells where this is true.
Then it needs to continue checking the same row on Sheet2 against the list of names on Sheet1 to find additional matches and do the same actions.



Sub Highlight_Calendar()

Dim lRow1 As Long
lRow1 = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row
Dim lRow2 As Long
lRow2 = Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count, 1).End(xlUp).Row
Dim lCol2 As Long
lCol2 = Worksheets("Sheet2").Cells(lRow2, Worksheets("Sheet2").Columns.Count).End(xlToLeft).Column
Dim ArrS2Names() As Variant
ArrS2Names = Sheet2.Range("A3", Worksheets("Sheet2").Cells(lRow2, 1))
Dim ArrS1Names() As Variant
ArrS1Names = Sheet1.Range("A3", Worksheets("Sheet1").Cells(lRow1, 1))
Dim calendarArr() As Variant
calendarArr = Sheet2.Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2))
Dim firstArr() As Variant
firstArr = Sheet1.Range("C3:C" & lRow1)
Dim lastArr() As Variant
lastArr = Sheet1.Range("D3:D" & lRow1)

Dim R1 As Long
Dim R2 As Long
Dim C2 As Long

For R2 = LBound(ArrS2Names, 1) To UBound(ArrS2Names, 1)
For R1 = LBound(ArrS1Names, 1) To UBound(ArrS1Names, 1)
For C2 = LBound(calendarArr, 2) To UBound(calendarArr, 2)
If ArrS2Names(R2, 1) = ArrS1Names(R1, 1) Then
Debug.Print (ArrS2Names(R2, 1))
If calendarArr(R2, C2) >= firstArr(R1, 1) And calendarArr(R2, C2) <= lastArr(R1, 1) Then
Sheet2.Cells(R2, C2).Interior.Color = RGB(0, 255, 0)
Debug.Print (Sheet2.Cells(R2, C2))
End If
End If
Next C2
Next R1
Next R2
End Sub






excel vba for-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 2:59







S.Hawkes

















asked Nov 15 '18 at 22:40









S.HawkesS.Hawkes

12




12












  • This would be easier to visualize with some mock data. Would you be able to edit your question and post a link to relevant screen captures?

    – cybernetic.nomad
    Nov 15 '18 at 22:43











  • Since ArrS2Names is an array, ArrS2Names(R2, "A") will fail. Maybe ArrS2Names(R2, 1) instead?

    – chris neilsen
    Nov 15 '18 at 22:59











  • Thanks I have edited the sheet to contain your suggestions, I am still getting a runtime error though even after changing all of the column letters for their corresponding numbers. I have also added the links (hopefully correctly) so everyone can see the spreadsheets I am working with. When I "debug" the error it highlights line 4 or "lRow1 = Worksheets("Sheet1").Range(Worksheets("Sheet1").Rows.Count, Worksheets("Sheet1").Columns("A")).End(x1Up).Row" not sure if this is significant?

    – S.Hawkes
    Nov 15 '18 at 23:06












  • Where is the code throwing the error?

    – a-burge
    Nov 15 '18 at 23:07











  • Change that line to Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row Note you have x1Up (ie x One Up) should be xlUp (ie x El Up) You will need to make similar changes elsewhere too

    – chris neilsen
    Nov 15 '18 at 23:13


















  • This would be easier to visualize with some mock data. Would you be able to edit your question and post a link to relevant screen captures?

    – cybernetic.nomad
    Nov 15 '18 at 22:43











  • Since ArrS2Names is an array, ArrS2Names(R2, "A") will fail. Maybe ArrS2Names(R2, 1) instead?

    – chris neilsen
    Nov 15 '18 at 22:59











  • Thanks I have edited the sheet to contain your suggestions, I am still getting a runtime error though even after changing all of the column letters for their corresponding numbers. I have also added the links (hopefully correctly) so everyone can see the spreadsheets I am working with. When I "debug" the error it highlights line 4 or "lRow1 = Worksheets("Sheet1").Range(Worksheets("Sheet1").Rows.Count, Worksheets("Sheet1").Columns("A")).End(x1Up).Row" not sure if this is significant?

    – S.Hawkes
    Nov 15 '18 at 23:06












  • Where is the code throwing the error?

    – a-burge
    Nov 15 '18 at 23:07











  • Change that line to Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row Note you have x1Up (ie x One Up) should be xlUp (ie x El Up) You will need to make similar changes elsewhere too

    – chris neilsen
    Nov 15 '18 at 23:13

















This would be easier to visualize with some mock data. Would you be able to edit your question and post a link to relevant screen captures?

– cybernetic.nomad
Nov 15 '18 at 22:43





This would be easier to visualize with some mock data. Would you be able to edit your question and post a link to relevant screen captures?

– cybernetic.nomad
Nov 15 '18 at 22:43













Since ArrS2Names is an array, ArrS2Names(R2, "A") will fail. Maybe ArrS2Names(R2, 1) instead?

– chris neilsen
Nov 15 '18 at 22:59





Since ArrS2Names is an array, ArrS2Names(R2, "A") will fail. Maybe ArrS2Names(R2, 1) instead?

– chris neilsen
Nov 15 '18 at 22:59













Thanks I have edited the sheet to contain your suggestions, I am still getting a runtime error though even after changing all of the column letters for their corresponding numbers. I have also added the links (hopefully correctly) so everyone can see the spreadsheets I am working with. When I "debug" the error it highlights line 4 or "lRow1 = Worksheets("Sheet1").Range(Worksheets("Sheet1").Rows.Count, Worksheets("Sheet1").Columns("A")).End(x1Up).Row" not sure if this is significant?

– S.Hawkes
Nov 15 '18 at 23:06






Thanks I have edited the sheet to contain your suggestions, I am still getting a runtime error though even after changing all of the column letters for their corresponding numbers. I have also added the links (hopefully correctly) so everyone can see the spreadsheets I am working with. When I "debug" the error it highlights line 4 or "lRow1 = Worksheets("Sheet1").Range(Worksheets("Sheet1").Rows.Count, Worksheets("Sheet1").Columns("A")).End(x1Up).Row" not sure if this is significant?

– S.Hawkes
Nov 15 '18 at 23:06














Where is the code throwing the error?

– a-burge
Nov 15 '18 at 23:07





Where is the code throwing the error?

– a-burge
Nov 15 '18 at 23:07













Change that line to Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row Note you have x1Up (ie x One Up) should be xlUp (ie x El Up) You will need to make similar changes elsewhere too

– chris neilsen
Nov 15 '18 at 23:13






Change that line to Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row Note you have x1Up (ie x One Up) should be xlUp (ie x El Up) You will need to make similar changes elsewhere too

– chris neilsen
Nov 15 '18 at 23:13













1 Answer
1






active

oldest

votes


















0














Whoop!! I have finally found the answers I needed for this, and while it is fairly simple functionally, I had no idea what questions to ask so it has been a rather gruelling task to complete. For anyone who comes after, hopefully my code will help answer some questions.



A really big thank you to all who helped and a special thank you to Chris Neilson for giving me the guidance and clarity to find my own answers. You may never know how much your comment of "do more research on how Range works" actually helped. I didn't realise how little I understood about ranges. Unfortunately I didn't keep a copy of the first code I posted, so the one in the question is fairly close to the final result due to edits.
I am not sure how to vote up discussions yet, but will look into this and vote up those who helped.



Sub Highlight_Calendar()

Dim lRow1 As Long
lRow1 = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row
Dim lRow2 As Long
lRow2 = Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count, 1).End(xlUp).Row
Dim lCol2 As Long
lCol2 = Worksheets("Sheet2").Cells(lRow2, Worksheets("Sheet2").Columns.Count).End(xlToLeft).Column
Dim ArrS2Names() As Variant
ArrS2Names = Sheet2.Range("A3", Worksheets("Sheet2").Cells(lRow2, 1))
Dim ArrS1Names() As Variant
ArrS1Names = Sheet1.Range("A3", Worksheets("Sheet1").Cells(lRow1, 1))
Dim calendarArr() As Variant
calendarArr = Sheet2.Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2))
Dim firstArr() As Variant
firstArr = Sheet1.Range("C3:C" & lRow1)
Dim lastArr() As Variant
lastArr = Sheet1.Range("D3:D" & lRow1)

Dim R1 As Long
Dim R2 As Long
Dim C2 As Long

For R2 = LBound(ArrS2Names, 1) To UBound(ArrS2Names, 1)
For R1 = LBound(ArrS1Names, 1) To UBound(ArrS1Names, 1)
For C2 = LBound(calendarArr, 2) To UBound(calendarArr, 2)
If ArrS2Names(R2, 1) = ArrS1Names(R1, 1) Then
Debug.Print (ArrS2Names(R2, 1))
If calendarArr(R2, C2) >= firstArr(R1, 1) And calendarArr(R2, C2) <= lastArr(R1, 1) Then
Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2)).Cells(R2, C2).Interior.Color = RGB(0, 255, 0)
Debug.Print Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2)).Cells(R2, C2)
End If
End If
Next C2
Next R1
Next R2
End Sub





share|improve this answer






















    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',
    autoActivateHeartbeat: false,
    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%2f53328884%2f2d-for-loop-used-on-a-staff-leave-calendar%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









    0














    Whoop!! I have finally found the answers I needed for this, and while it is fairly simple functionally, I had no idea what questions to ask so it has been a rather gruelling task to complete. For anyone who comes after, hopefully my code will help answer some questions.



    A really big thank you to all who helped and a special thank you to Chris Neilson for giving me the guidance and clarity to find my own answers. You may never know how much your comment of "do more research on how Range works" actually helped. I didn't realise how little I understood about ranges. Unfortunately I didn't keep a copy of the first code I posted, so the one in the question is fairly close to the final result due to edits.
    I am not sure how to vote up discussions yet, but will look into this and vote up those who helped.



    Sub Highlight_Calendar()

    Dim lRow1 As Long
    lRow1 = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row
    Dim lRow2 As Long
    lRow2 = Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count, 1).End(xlUp).Row
    Dim lCol2 As Long
    lCol2 = Worksheets("Sheet2").Cells(lRow2, Worksheets("Sheet2").Columns.Count).End(xlToLeft).Column
    Dim ArrS2Names() As Variant
    ArrS2Names = Sheet2.Range("A3", Worksheets("Sheet2").Cells(lRow2, 1))
    Dim ArrS1Names() As Variant
    ArrS1Names = Sheet1.Range("A3", Worksheets("Sheet1").Cells(lRow1, 1))
    Dim calendarArr() As Variant
    calendarArr = Sheet2.Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2))
    Dim firstArr() As Variant
    firstArr = Sheet1.Range("C3:C" & lRow1)
    Dim lastArr() As Variant
    lastArr = Sheet1.Range("D3:D" & lRow1)

    Dim R1 As Long
    Dim R2 As Long
    Dim C2 As Long

    For R2 = LBound(ArrS2Names, 1) To UBound(ArrS2Names, 1)
    For R1 = LBound(ArrS1Names, 1) To UBound(ArrS1Names, 1)
    For C2 = LBound(calendarArr, 2) To UBound(calendarArr, 2)
    If ArrS2Names(R2, 1) = ArrS1Names(R1, 1) Then
    Debug.Print (ArrS2Names(R2, 1))
    If calendarArr(R2, C2) >= firstArr(R1, 1) And calendarArr(R2, C2) <= lastArr(R1, 1) Then
    Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2)).Cells(R2, C2).Interior.Color = RGB(0, 255, 0)
    Debug.Print Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2)).Cells(R2, C2)
    End If
    End If
    Next C2
    Next R1
    Next R2
    End Sub





    share|improve this answer



























      0














      Whoop!! I have finally found the answers I needed for this, and while it is fairly simple functionally, I had no idea what questions to ask so it has been a rather gruelling task to complete. For anyone who comes after, hopefully my code will help answer some questions.



      A really big thank you to all who helped and a special thank you to Chris Neilson for giving me the guidance and clarity to find my own answers. You may never know how much your comment of "do more research on how Range works" actually helped. I didn't realise how little I understood about ranges. Unfortunately I didn't keep a copy of the first code I posted, so the one in the question is fairly close to the final result due to edits.
      I am not sure how to vote up discussions yet, but will look into this and vote up those who helped.



      Sub Highlight_Calendar()

      Dim lRow1 As Long
      lRow1 = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row
      Dim lRow2 As Long
      lRow2 = Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count, 1).End(xlUp).Row
      Dim lCol2 As Long
      lCol2 = Worksheets("Sheet2").Cells(lRow2, Worksheets("Sheet2").Columns.Count).End(xlToLeft).Column
      Dim ArrS2Names() As Variant
      ArrS2Names = Sheet2.Range("A3", Worksheets("Sheet2").Cells(lRow2, 1))
      Dim ArrS1Names() As Variant
      ArrS1Names = Sheet1.Range("A3", Worksheets("Sheet1").Cells(lRow1, 1))
      Dim calendarArr() As Variant
      calendarArr = Sheet2.Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2))
      Dim firstArr() As Variant
      firstArr = Sheet1.Range("C3:C" & lRow1)
      Dim lastArr() As Variant
      lastArr = Sheet1.Range("D3:D" & lRow1)

      Dim R1 As Long
      Dim R2 As Long
      Dim C2 As Long

      For R2 = LBound(ArrS2Names, 1) To UBound(ArrS2Names, 1)
      For R1 = LBound(ArrS1Names, 1) To UBound(ArrS1Names, 1)
      For C2 = LBound(calendarArr, 2) To UBound(calendarArr, 2)
      If ArrS2Names(R2, 1) = ArrS1Names(R1, 1) Then
      Debug.Print (ArrS2Names(R2, 1))
      If calendarArr(R2, C2) >= firstArr(R1, 1) And calendarArr(R2, C2) <= lastArr(R1, 1) Then
      Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2)).Cells(R2, C2).Interior.Color = RGB(0, 255, 0)
      Debug.Print Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2)).Cells(R2, C2)
      End If
      End If
      Next C2
      Next R1
      Next R2
      End Sub





      share|improve this answer

























        0












        0








        0







        Whoop!! I have finally found the answers I needed for this, and while it is fairly simple functionally, I had no idea what questions to ask so it has been a rather gruelling task to complete. For anyone who comes after, hopefully my code will help answer some questions.



        A really big thank you to all who helped and a special thank you to Chris Neilson for giving me the guidance and clarity to find my own answers. You may never know how much your comment of "do more research on how Range works" actually helped. I didn't realise how little I understood about ranges. Unfortunately I didn't keep a copy of the first code I posted, so the one in the question is fairly close to the final result due to edits.
        I am not sure how to vote up discussions yet, but will look into this and vote up those who helped.



        Sub Highlight_Calendar()

        Dim lRow1 As Long
        lRow1 = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row
        Dim lRow2 As Long
        lRow2 = Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count, 1).End(xlUp).Row
        Dim lCol2 As Long
        lCol2 = Worksheets("Sheet2").Cells(lRow2, Worksheets("Sheet2").Columns.Count).End(xlToLeft).Column
        Dim ArrS2Names() As Variant
        ArrS2Names = Sheet2.Range("A3", Worksheets("Sheet2").Cells(lRow2, 1))
        Dim ArrS1Names() As Variant
        ArrS1Names = Sheet1.Range("A3", Worksheets("Sheet1").Cells(lRow1, 1))
        Dim calendarArr() As Variant
        calendarArr = Sheet2.Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2))
        Dim firstArr() As Variant
        firstArr = Sheet1.Range("C3:C" & lRow1)
        Dim lastArr() As Variant
        lastArr = Sheet1.Range("D3:D" & lRow1)

        Dim R1 As Long
        Dim R2 As Long
        Dim C2 As Long

        For R2 = LBound(ArrS2Names, 1) To UBound(ArrS2Names, 1)
        For R1 = LBound(ArrS1Names, 1) To UBound(ArrS1Names, 1)
        For C2 = LBound(calendarArr, 2) To UBound(calendarArr, 2)
        If ArrS2Names(R2, 1) = ArrS1Names(R1, 1) Then
        Debug.Print (ArrS2Names(R2, 1))
        If calendarArr(R2, C2) >= firstArr(R1, 1) And calendarArr(R2, C2) <= lastArr(R1, 1) Then
        Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2)).Cells(R2, C2).Interior.Color = RGB(0, 255, 0)
        Debug.Print Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2)).Cells(R2, C2)
        End If
        End If
        Next C2
        Next R1
        Next R2
        End Sub





        share|improve this answer













        Whoop!! I have finally found the answers I needed for this, and while it is fairly simple functionally, I had no idea what questions to ask so it has been a rather gruelling task to complete. For anyone who comes after, hopefully my code will help answer some questions.



        A really big thank you to all who helped and a special thank you to Chris Neilson for giving me the guidance and clarity to find my own answers. You may never know how much your comment of "do more research on how Range works" actually helped. I didn't realise how little I understood about ranges. Unfortunately I didn't keep a copy of the first code I posted, so the one in the question is fairly close to the final result due to edits.
        I am not sure how to vote up discussions yet, but will look into this and vote up those who helped.



        Sub Highlight_Calendar()

        Dim lRow1 As Long
        lRow1 = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row
        Dim lRow2 As Long
        lRow2 = Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count, 1).End(xlUp).Row
        Dim lCol2 As Long
        lCol2 = Worksheets("Sheet2").Cells(lRow2, Worksheets("Sheet2").Columns.Count).End(xlToLeft).Column
        Dim ArrS2Names() As Variant
        ArrS2Names = Sheet2.Range("A3", Worksheets("Sheet2").Cells(lRow2, 1))
        Dim ArrS1Names() As Variant
        ArrS1Names = Sheet1.Range("A3", Worksheets("Sheet1").Cells(lRow1, 1))
        Dim calendarArr() As Variant
        calendarArr = Sheet2.Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2))
        Dim firstArr() As Variant
        firstArr = Sheet1.Range("C3:C" & lRow1)
        Dim lastArr() As Variant
        lastArr = Sheet1.Range("D3:D" & lRow1)

        Dim R1 As Long
        Dim R2 As Long
        Dim C2 As Long

        For R2 = LBound(ArrS2Names, 1) To UBound(ArrS2Names, 1)
        For R1 = LBound(ArrS1Names, 1) To UBound(ArrS1Names, 1)
        For C2 = LBound(calendarArr, 2) To UBound(calendarArr, 2)
        If ArrS2Names(R2, 1) = ArrS1Names(R1, 1) Then
        Debug.Print (ArrS2Names(R2, 1))
        If calendarArr(R2, C2) >= firstArr(R1, 1) And calendarArr(R2, C2) <= lastArr(R1, 1) Then
        Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2)).Cells(R2, C2).Interior.Color = RGB(0, 255, 0)
        Debug.Print Range("B3", Worksheets("Sheet2").Cells(lRow2, lCol2)).Cells(R2, C2)
        End If
        End If
        Next C2
        Next R1
        Next R2
        End Sub






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 4:12









        S.HawkesS.Hawkes

        12




        12





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328884%2f2d-for-loop-used-on-a-staff-leave-calendar%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

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            Evgeni Malkin