How to Open a Worksheet at TODAY()+2?












1















I have multiple Excel worksheets with dates in col B. I use the following VBA code block to open the worksheet with today's date visible on screen.



Private Sub Worksheet_Activate()

Dim d As Date, i As Long

d = Date

ActiveWindow.ScrollRow = 1

For i = 16 To Cells(Rows.Count, "B").End(xlUp).Row
If Cells(i, "B").Value = d Then
Cells(i, "B").Select
Exit Sub
End If
Next

End Sub


The date column does not contain dates for the weekend so the coding only works when today is between Monday and Friday. If I open the sheet on a Saturday it opens at the top of the sheet, which can be thousands of rows away from today's date as the sheet contains historical data dating back several years.



How do I amend the coding so that if today's date is not in col B it increments today's date by 2 and searches for the VBA equivalent of TODAY()+2, so that it will open the sheet with Monday's date visible if today is Saturday, or Tuesday's date visible if today is Sunday?










share|improve this question



























    1















    I have multiple Excel worksheets with dates in col B. I use the following VBA code block to open the worksheet with today's date visible on screen.



    Private Sub Worksheet_Activate()

    Dim d As Date, i As Long

    d = Date

    ActiveWindow.ScrollRow = 1

    For i = 16 To Cells(Rows.Count, "B").End(xlUp).Row
    If Cells(i, "B").Value = d Then
    Cells(i, "B").Select
    Exit Sub
    End If
    Next

    End Sub


    The date column does not contain dates for the weekend so the coding only works when today is between Monday and Friday. If I open the sheet on a Saturday it opens at the top of the sheet, which can be thousands of rows away from today's date as the sheet contains historical data dating back several years.



    How do I amend the coding so that if today's date is not in col B it increments today's date by 2 and searches for the VBA equivalent of TODAY()+2, so that it will open the sheet with Monday's date visible if today is Saturday, or Tuesday's date visible if today is Sunday?










    share|improve this question

























      1












      1








      1








      I have multiple Excel worksheets with dates in col B. I use the following VBA code block to open the worksheet with today's date visible on screen.



      Private Sub Worksheet_Activate()

      Dim d As Date, i As Long

      d = Date

      ActiveWindow.ScrollRow = 1

      For i = 16 To Cells(Rows.Count, "B").End(xlUp).Row
      If Cells(i, "B").Value = d Then
      Cells(i, "B").Select
      Exit Sub
      End If
      Next

      End Sub


      The date column does not contain dates for the weekend so the coding only works when today is between Monday and Friday. If I open the sheet on a Saturday it opens at the top of the sheet, which can be thousands of rows away from today's date as the sheet contains historical data dating back several years.



      How do I amend the coding so that if today's date is not in col B it increments today's date by 2 and searches for the VBA equivalent of TODAY()+2, so that it will open the sheet with Monday's date visible if today is Saturday, or Tuesday's date visible if today is Sunday?










      share|improve this question














      I have multiple Excel worksheets with dates in col B. I use the following VBA code block to open the worksheet with today's date visible on screen.



      Private Sub Worksheet_Activate()

      Dim d As Date, i As Long

      d = Date

      ActiveWindow.ScrollRow = 1

      For i = 16 To Cells(Rows.Count, "B").End(xlUp).Row
      If Cells(i, "B").Value = d Then
      Cells(i, "B").Select
      Exit Sub
      End If
      Next

      End Sub


      The date column does not contain dates for the weekend so the coding only works when today is between Monday and Friday. If I open the sheet on a Saturday it opens at the top of the sheet, which can be thousands of rows away from today's date as the sheet contains historical data dating back several years.



      How do I amend the coding so that if today's date is not in col B it increments today's date by 2 and searches for the VBA equivalent of TODAY()+2, so that it will open the sheet with Monday's date visible if today is Saturday, or Tuesday's date visible if today is Sunday?







      excel vba






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 23:27









      OldFellaOldFella

      112




      112
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Not sure if this is what you're looking for, but if it's a weekend, it'll increment the date by two; otherwise it will remain as today's date.



          Replace:



          d = Date



          with



          d=Evaluate("=IF(OR(WEEKDAY(TODAY())=7,WEEKDAY(TODAY())=1),TODAY()+2,TODAY())")



          But also you may want to reconsider looping through every cell to find what you're looking for. If you have a large dataset it might take a while consider replacing it with this:



          If Application.WorksheetFunction.CountIf(ActiveSheet.Range("B:B"), d) > 0 Then
          ActiveSheet.Range("B:B").Find(d).Activate
          End If


          the countif just ensures that the cell value actually exists and doesn't error when you try to activate it.






          share|improve this answer

































            0














            Disregard! After more experimenting I've managed to resolve it, it simply needed 1 more line of code:



            Private Sub Worksheet_Activate()

            Dim d As Date, i As Long

            d = Date

            ActiveWindow.ScrollRow = 1

            For i = 16 To Cells(Rows.Count, "B").End(xlUp).Row
            If Cells(i, "B").Value = d Then
            Cells(i, "B").Select
            Exit Sub
            Else: d = Date + 2

            End If
            Next

            End Sub





            share|improve this answer
























            • I feel like this might not work all the time. Imagine a scenario where its Saturday, and B16 contains Monday's date. If you run this, it'll increment d by two, but if B17 doesn't also contain Monday's date it won't match and it'll increment to Wednesday's date and it'll continue on like that. A similar thing would happen during the weekdays if B16 didn't contain the current day's date.

              – Brotato
              Nov 16 '18 at 23:59













            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%2f53346678%2fhow-to-open-a-worksheet-at-today2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Not sure if this is what you're looking for, but if it's a weekend, it'll increment the date by two; otherwise it will remain as today's date.



            Replace:



            d = Date



            with



            d=Evaluate("=IF(OR(WEEKDAY(TODAY())=7,WEEKDAY(TODAY())=1),TODAY()+2,TODAY())")



            But also you may want to reconsider looping through every cell to find what you're looking for. If you have a large dataset it might take a while consider replacing it with this:



            If Application.WorksheetFunction.CountIf(ActiveSheet.Range("B:B"), d) > 0 Then
            ActiveSheet.Range("B:B").Find(d).Activate
            End If


            the countif just ensures that the cell value actually exists and doesn't error when you try to activate it.






            share|improve this answer






























              1














              Not sure if this is what you're looking for, but if it's a weekend, it'll increment the date by two; otherwise it will remain as today's date.



              Replace:



              d = Date



              with



              d=Evaluate("=IF(OR(WEEKDAY(TODAY())=7,WEEKDAY(TODAY())=1),TODAY()+2,TODAY())")



              But also you may want to reconsider looping through every cell to find what you're looking for. If you have a large dataset it might take a while consider replacing it with this:



              If Application.WorksheetFunction.CountIf(ActiveSheet.Range("B:B"), d) > 0 Then
              ActiveSheet.Range("B:B").Find(d).Activate
              End If


              the countif just ensures that the cell value actually exists and doesn't error when you try to activate it.






              share|improve this answer




























                1












                1








                1







                Not sure if this is what you're looking for, but if it's a weekend, it'll increment the date by two; otherwise it will remain as today's date.



                Replace:



                d = Date



                with



                d=Evaluate("=IF(OR(WEEKDAY(TODAY())=7,WEEKDAY(TODAY())=1),TODAY()+2,TODAY())")



                But also you may want to reconsider looping through every cell to find what you're looking for. If you have a large dataset it might take a while consider replacing it with this:



                If Application.WorksheetFunction.CountIf(ActiveSheet.Range("B:B"), d) > 0 Then
                ActiveSheet.Range("B:B").Find(d).Activate
                End If


                the countif just ensures that the cell value actually exists and doesn't error when you try to activate it.






                share|improve this answer















                Not sure if this is what you're looking for, but if it's a weekend, it'll increment the date by two; otherwise it will remain as today's date.



                Replace:



                d = Date



                with



                d=Evaluate("=IF(OR(WEEKDAY(TODAY())=7,WEEKDAY(TODAY())=1),TODAY()+2,TODAY())")



                But also you may want to reconsider looping through every cell to find what you're looking for. If you have a large dataset it might take a while consider replacing it with this:



                If Application.WorksheetFunction.CountIf(ActiveSheet.Range("B:B"), d) > 0 Then
                ActiveSheet.Range("B:B").Find(d).Activate
                End If


                the countif just ensures that the cell value actually exists and doesn't error when you try to activate it.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 16 '18 at 23:50

























                answered Nov 16 '18 at 23:43









                BrotatoBrotato

                800212




                800212

























                    0














                    Disregard! After more experimenting I've managed to resolve it, it simply needed 1 more line of code:



                    Private Sub Worksheet_Activate()

                    Dim d As Date, i As Long

                    d = Date

                    ActiveWindow.ScrollRow = 1

                    For i = 16 To Cells(Rows.Count, "B").End(xlUp).Row
                    If Cells(i, "B").Value = d Then
                    Cells(i, "B").Select
                    Exit Sub
                    Else: d = Date + 2

                    End If
                    Next

                    End Sub





                    share|improve this answer
























                    • I feel like this might not work all the time. Imagine a scenario where its Saturday, and B16 contains Monday's date. If you run this, it'll increment d by two, but if B17 doesn't also contain Monday's date it won't match and it'll increment to Wednesday's date and it'll continue on like that. A similar thing would happen during the weekdays if B16 didn't contain the current day's date.

                      – Brotato
                      Nov 16 '18 at 23:59


















                    0














                    Disregard! After more experimenting I've managed to resolve it, it simply needed 1 more line of code:



                    Private Sub Worksheet_Activate()

                    Dim d As Date, i As Long

                    d = Date

                    ActiveWindow.ScrollRow = 1

                    For i = 16 To Cells(Rows.Count, "B").End(xlUp).Row
                    If Cells(i, "B").Value = d Then
                    Cells(i, "B").Select
                    Exit Sub
                    Else: d = Date + 2

                    End If
                    Next

                    End Sub





                    share|improve this answer
























                    • I feel like this might not work all the time. Imagine a scenario where its Saturday, and B16 contains Monday's date. If you run this, it'll increment d by two, but if B17 doesn't also contain Monday's date it won't match and it'll increment to Wednesday's date and it'll continue on like that. A similar thing would happen during the weekdays if B16 didn't contain the current day's date.

                      – Brotato
                      Nov 16 '18 at 23:59
















                    0












                    0








                    0







                    Disregard! After more experimenting I've managed to resolve it, it simply needed 1 more line of code:



                    Private Sub Worksheet_Activate()

                    Dim d As Date, i As Long

                    d = Date

                    ActiveWindow.ScrollRow = 1

                    For i = 16 To Cells(Rows.Count, "B").End(xlUp).Row
                    If Cells(i, "B").Value = d Then
                    Cells(i, "B").Select
                    Exit Sub
                    Else: d = Date + 2

                    End If
                    Next

                    End Sub





                    share|improve this answer













                    Disregard! After more experimenting I've managed to resolve it, it simply needed 1 more line of code:



                    Private Sub Worksheet_Activate()

                    Dim d As Date, i As Long

                    d = Date

                    ActiveWindow.ScrollRow = 1

                    For i = 16 To Cells(Rows.Count, "B").End(xlUp).Row
                    If Cells(i, "B").Value = d Then
                    Cells(i, "B").Select
                    Exit Sub
                    Else: d = Date + 2

                    End If
                    Next

                    End Sub






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 '18 at 23:49









                    OldFellaOldFella

                    112




                    112













                    • I feel like this might not work all the time. Imagine a scenario where its Saturday, and B16 contains Monday's date. If you run this, it'll increment d by two, but if B17 doesn't also contain Monday's date it won't match and it'll increment to Wednesday's date and it'll continue on like that. A similar thing would happen during the weekdays if B16 didn't contain the current day's date.

                      – Brotato
                      Nov 16 '18 at 23:59





















                    • I feel like this might not work all the time. Imagine a scenario where its Saturday, and B16 contains Monday's date. If you run this, it'll increment d by two, but if B17 doesn't also contain Monday's date it won't match and it'll increment to Wednesday's date and it'll continue on like that. A similar thing would happen during the weekdays if B16 didn't contain the current day's date.

                      – Brotato
                      Nov 16 '18 at 23:59



















                    I feel like this might not work all the time. Imagine a scenario where its Saturday, and B16 contains Monday's date. If you run this, it'll increment d by two, but if B17 doesn't also contain Monday's date it won't match and it'll increment to Wednesday's date and it'll continue on like that. A similar thing would happen during the weekdays if B16 didn't contain the current day's date.

                    – Brotato
                    Nov 16 '18 at 23:59







                    I feel like this might not work all the time. Imagine a scenario where its Saturday, and B16 contains Monday's date. If you run this, it'll increment d by two, but if B17 doesn't also contain Monday's date it won't match and it'll increment to Wednesday's date and it'll continue on like that. A similar thing would happen during the weekdays if B16 didn't contain the current day's date.

                    – Brotato
                    Nov 16 '18 at 23:59




















                    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%2f53346678%2fhow-to-open-a-worksheet-at-today2%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

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)