+ Reply to Thread
Results 1 to 9 of 9

VLookUp issue

Hybrid View

  1. #1
    Registered User
    Join Date
    01-09-2013
    Location
    Mississauga, Canada
    MS-Off Ver
    Excel 2007
    Posts
    8

    VLookUp issue

    Hi all,

    Here's the story. I have a table whose first column's got all the dates of the year (all 365 of them) and a second column that has an integer number (assigned to each date). I am trying to use the VLookUp function in VBA to search today's date within the first column, and return its corresponding integer value. Well, today's not my day.

    I want to use this value to refer to a column in another worksheet (in this sheet, the columns are labelled across by these integer values).

    My Lookup function just won't work. Anyone bold enough to tackle this problem?

    Thanks a bunch.
    lunaticlion

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,395

    Re: VLookUp issue

    That shouldn't be difficult. We can't really tell you what might be wrong with your formula without seeing it, and, if the formul looks OK, we'd need to see the data.

    So, best bet, upload a sample workbook.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    01-09-2013
    Location
    Mississauga, Canada
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VLookUp issue

    Hi TMS,

    I've uploaded a mock workbook. Kindly take a look. I'm sure it's the way I'm calling the function that's causing the issue.
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    09-04-2007
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    133

    Re: VLookUp issue

    Hey,

    This thread might be helpful.

    http://www.excelforum.com/excel-prog...or-1004-a.html

  5. #5
    Valued Forum Contributor
    Join Date
    07-29-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    534

    Re: VLookUp issue

    In your macro "BossScript" i inserted the following (red) line of code to check the value of variable "deliverDate"
    deliverDate = sheet0.Range("D5").End(xlDown).Value
        Debug.Print deliverDate
    In the Immediate window, the result was:
    ?6/11/1883 6:51:26
    Maybe not what you expected?

  6. #6
    Registered User
    Join Date
    01-09-2013
    Location
    Mississauga, Canada
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VLookUp issue

    WHER,

    You're right, not what I expected at all. I was trying to assign each value contained in the column of D5 (in sheet "Main") to "deliverDate" one by one. The code was my attempt to select a dynamic range, and probably goofed. Any tips?

    LL

  7. #7
    Registered User
    Join Date
    01-09-2013
    Location
    Mississauga, Canada
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VLookUp issue

    I got the VLookup function to work in one module, but doesn't work in another module of the same workbook :S

    This works:
    Sub Test()
        Dim testdate As Long
        
        Worksheets("Main").Activate
        testdate = CLng(Worksheets("Main").Range("D78").Value)
        
        ColumnSelection (testdate)
        
    End Sub
    
    Sub ColumnSelection(Sundate As Long)
        
        Dim weekNum As Variant
        
        Worksheets("Calendar 2013").Activate
        weekNum = WorksheetFunction.VLookup(Sundate, Range("A:B"), 2, False)
        
        MsgBox weekNum
           
        Worksheets("Main").Activate
        
        Dim weekNumRow As Variant
        Dim ColSelect As Range
        Dim i As Integer
        
        weekNumRow = Range("M4:BM4").Value
        
        'MsgBox weekNumRow(1, 8)
        
        For i = 1 To 53
            If weekNumRow(1, i) = weekNum Then
                Range("M4:BM4").Cells(1, i).Activate
                Set ColSelect = Range(ActiveCell.Offset(1, 0), ActiveCell.End(xlDown).Offset(-1, 0))
                MsgBox "Value is in column " & ColSelect.Address
                Exit For
            End If
        Next i
        
    End Sub
    But this doesn't:

    Public Sub BossScript()
    
        Dim Sundate, fabDate, truncValue As Long
        Dim fabRange, whoRange, fracRange, truncRange As Range
        Dim whoValue, fracValue As Variant
        Dim w, f, t As Integer
        
        Worksheets("Main").Activate
        Sundate = CLng(Worksheets("Main").Range("A1").Value)
        
        Set fabRange = Range(Cells(5, 5), Cells(5, 5).End(xlDown))
        Set whoRange = Range(Cells(5, 11), Cells(5, 11).End(xlDown))
        Set truncRange = Range(Cells(5, 7), Cells(5, 7).End(xlDown))
        
        For w = 1 To whoRange.Rows.Count
            fabDate = CLng(fabRange.Cells(w, 1).Value)
            whoValue = whoRange.Cells(w, 1).Value
            truncValue = CLng(Int(truncRange.Cells(w, 1).Value))
            If whoValue > 0 Then
                If fabDate < Sundate Then
                ColumnSelection (fabDate)
                Else
                ColumnSelection (Sundate)
                End If
            End If
                        
            'MsgBox whoRange.Address
            'MsgBox whoValue
        Next w
        
        'whoRange = Range(K5, K5.End(xlDown)).Value
        
    End Sub
    
    Sub ColumnSelection(selectDate As Long)
    
        Dim weekNum As Variant
    
        Worksheets("Calendar 2013").Activate
        weekNum = WorksheetFunction.VLookup(selectDate, Range("A:B"), 2, False)
        
        MsgBox weekNum
           
        Worksheets("Main").Activate
        
        Dim weekNumRow As Variant
        Dim ColSelect As Long
        Dim i As Integer
        
        weekNumRow = Range("M4:BM4").Value
        
        'MsgBox weekNumRow(1, 8)
        
        For i = 1 To 53
            If weekNumRow(1, i) = weekNum Then
                Range("M4:BM4").Cells(1, i).Activate
                ColSelect = Range(ActiveCell.Offset(1, 0), ActiveCell.End(xlDown).Offset(-1, 0)).Column
                'MsgBox "Value is in column " & ColSelect.Address
                Exit For
            End If
        Next i
        
        If truncValue > 0 Then
            For t = 0 To truncValue - 1
                Range(Cells(w, 11)).Copy Range(Cells(w, ColSelect + t))
            Next t
        End If
    End Sub
    Help, please!
    Last edited by lionlunatication; 01-20-2013 at 07:17 PM.

  8. #8
    Registered User
    Join Date
    01-09-2013
    Location
    Mississauga, Canada
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VLookUp issue

    Nvm, solved it! Just conflicting conditional statements...

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,395

    Re: VLookUp issue

    Thanks for letting us know. Glad you have a solution.


    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1