Results 1 to 8 of 8

Thread: VBA code to find last entry on a certain date and bring back the text in the adjacent

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    VBA code to find last entry on a certain date and bring back the text in the adjacent

    Hi,

    I hope I'm making sense here.

    I have a spreadsheet with hundreds of sheets on it (one for each of my customers)

    Basically they each have an amount of money from which they can order goods from me. I need to be able to find out the balance of each fund on any given date in the past and wondered if there is a VBA code that create a summary sheet.

    There could be several entries for the same date, but at the same time there may be no entries for that date, therefore I need it to search for either the last entry on the date requested or the next date before this with an entry.

    I have attached an example speadsheet.

    Many thanks in advance.
    Attached Files Attached Files

  2. #2
    Senior Member alansidman's Avatar
    Join Date
    Apr 2012
    Posts
    125
    Rep Power
    13
    Here is some code that will search for the specific date and return the information only if the date is found. Not sure how to tweak this to find the next earlier date, but perhaps someone else can tweak this for you.

    Code:
    Option Explicit
    
    Sub Summary()
        Dim w As Worksheet
        Dim i As Long
        'Dim rng As Range
        Dim d As Date
        Dim lrng As Range
        Dim lr As Long
        d = InputBox("What Date to Search")
        'Dim wsFunc As WorksheetFunction
        'Set wsFunc = Application.WorksheetFunction
        
    
        For Each w In Worksheets
            If w.Name <> "Summary" Then
            'Set rng = w.Range("L6:L" & Range("L" & Rows.Count).End(xlUp).Row)
            Set lrng = w.Range("L:M")
            lr = Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Row
            
                For i = 6 To w.Range("L" & Rows.Count).End(xlUp).Row
                    On Error Resume Next
                    'If w.Range("L" & i) = wsFunc.VLookup(d, lrng, 2, False) Then
                    If w.Range("L" & i) = d Then
                    w.Range("L" & i).Resize(, 2).Copy
                    Sheets("Summary").Range("B" & lr + 1).PasteSpecial Paste:=xlValues
                    Sheets("Summary").Range("A" & lr + 1) = w.Name
                    End If
                Next i
            End If
        Next w
        MsgBox ("Complete")
    
    End Sub

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0
    Hi Alan,

    Thanks for the reply, however when I run the macro I get a Run-time error 9 - Subscript out of range.
    Last edited by Admin; 03-11-2014 at 07:43 AM. Reason: quote removed

  4. #4
    Senior Member alansidman's Avatar
    Join Date
    Apr 2012
    Posts
    125
    Rep Power
    13
    Which line of code is highlighted when you debug the code?

  5. #5
    Senior Member alansidman's Avatar
    Join Date
    Apr 2012
    Posts
    125
    Rep Power
    13
    Example Fund.xlsm

    Unable to duplicate your issue. Here is the file that I tested on.

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0
    Quote Originally Posted by pedros23 View Post
    Hi Alan,

    Thanks for the reply, however when I run the macro I get a Run-time error 9 - Subscript out of range.
    The following is highlighted

    lr = Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Row

  7. #7
    Senior Member alansidman's Avatar
    Join Date
    Apr 2012
    Posts
    125
    Rep Power
    13
    Do you have a sheet named Summary in your workbook? If not, then replace "Summary" with the name of your summary sheet as described in your first post to this thread. In your first thread you indicated you wanted the results in a Summary Sheet, but you did not have one in your test file. I created one and called it Summary. Whatever yours is called needs to be put in this line of code.

  8. #8
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0
    Quote Originally Posted by alansidman View Post
    Do you have a sheet named Summary in your workbook? If not, then replace "Summary" with the name of your summary sheet as described in your first post to this thread. In your first thread you indicated you wanted the results in a Summary Sheet, but you did not have one in your test file. I created one and called it Summary. Whatever yours is called needs to be put in this line of code.
    Thanks Alan,

    I had a feeling I had missed something simple. You're help has been fantastic.

    I just need to figure out how to make it find the next previous date and i will save me so much time.

Similar Threads

  1. data entry to correct cell range...code needs help
    By paul_pearson in forum Excel Help
    Replies: 2
    Last Post: 08-28-2013, 05:26 PM
  2. Replies: 6
    Last Post: 05-22-2013, 02:23 AM
  3. Vba Code to find value and paste on certain row
    By jwitte in forum Excel Help
    Replies: 3
    Last Post: 11-28-2012, 08:52 PM
  4. Date References to save files using VBA Code
    By mrmmickle1 in forum Excel Help
    Replies: 3
    Last Post: 11-28-2012, 05:48 PM
  5. Find The Last Entry Row In A Column That Contains Only Numbers
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 05-09-2011, 05:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •