Kamis, 03 Januari 2019

[ExcelVBA] Re: Open Worksheet to a current date in a column

 

Jim,


I went ahead and threw this together.
it works very quickly.
I use a technique I like to use that utilizes a "Dictionary Object".
I first Load a dictionary Object with the dates from column A.
this has the additional benefit of allowing you to have the dates in any order as long as they are unique!
I put the row number as the "value" of the dictionary lookup.

Then it's just a matter of doing some validation to make sure that the dates are inserted always in the same format and "today" is compared in the same format.
I converted them to a string format in "mm-dd-yyyy" format.

I tested it with 30 years of rows (10,950) and it was nearly instantaneous.
although it only had two columns...

In a "standard" module, the macro looks like:
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Option Explicit

Public Dict_Dates

Sub Today_Data()
    Dim nRows, nRow, sDate As String
    Set Dict_Dates = CreateObject("Scripting.Dictionary")
        Dict_Dates.RemoveAll
    nRows = Application.WorksheetFunction.CountA(Sheets(1).Range("A:A"))
    '---------------------------------------------------------------
    ' Loop through dates and store in Data Dictionary
    '---------------------------------------------------------------
    For nRow = 2 To nRows
        If (IsDate(Sheets(1).Cells(nRow, "A").Value)) Then
            sDate = Format(Sheets(1).Cells(nRow, "A").Value, "mm-dd-yyyy")
            If (Not Dict_Dates.exists(sDate)) Then
                    Dict_Dates.Add sDate, nRow
            Else
                MsgBox "Duplicate Date: " & Chr(13) & sDate & Chr(13) & " row: " & Dict_Dates.Item(sDate) & Chr(13) & " row: " & nRow
            End If
        End If
    Next nRow
    '---------------------------------------------------------------
    ' Find Today's Date
    '---------------------------------------------------------------
    sDate = Format(Now(), "mm-dd-yyyy")
    If (Not Dict_Dates.exists(sDate)) Then
        MsgBox "Today (" & sDate & ") is not in current list"
    Else
        nRow = Dict_Dates.Item(sDate)
        Cells(nRow, "B").Select
    End If

End Sub
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

In the "Thisworkbook" module, the Workbook_open sub looks like:

Private Sub Workbook_Open()
    Today_Data
End Sub

hope this helps.

~Paul

__._,_.___

Posted by: schreiner_paul@att.net
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (5)

Have you tried the highest rated email app?
With 4.5 stars in iTunes, the Yahoo Mail app is the highest rated email app on the market. What are you waiting for? Now you can access all your inboxes (Gmail, Outlook, AOL and more) in one place. Never delete an email again with 1000GB of free cloud storage.

----------------------------------
Visit our ExcelVBA group home page for more info and support files:
http://groups.yahoo.com/group/ExcelVBA
------------------------------------

SPONSORED LINKS
.

__,_._,___

Tidak ada komentar:

Posting Komentar